views:

768

answers:

3

I need to replace a button on my web page with a hyperlink. I am calling a PHP script using the button.

I extract the id using the following statement:

$id = $_POST['id'];

HTML code:

<form id="test1" method="post" action="my.php?action=show">
  <input type="hidden" name="id" id="id" value="1" />
  <input type="submit" name="submit" value="Click" onclick="return display(1);" />
</form>

Here is what I came up with:

 <a href="my.php?action=show&id='1'" onclick="return display(1);"> Click</a>

Does my code have a flaw? Is there a better approach?

A: 

No - looks fine to me, though the '1' doesn't need to be in inverted commas, andy you'll need to change your $_GET to $_POST in your first line of PHP.

Rich Bradshaw
The "inverted commas" are apostrophes.
Guffa
Ah - that's the word I was thinking of - wouldn't come to mind for some reason!
Rich Bradshaw
Actually, inverted commas is just as valid: http://en.wikipedia.org/wiki/Single_quotesAnd in programming, they're usually referred to as 'single quotes' rather than apostrophes/inverted commas.
Calvin
+3  A: 

Looks good, except for three things:

  1. Use &amp; instead of &.
  2. Use id=1 instead of id='1'.
  3. Use $_GET instead of $_POST. If you want backwards compatibility, you can opt for $_REQUEST.
strager
Pim Jager
strager
+1  A: 

You can make the link post the form:

<a href="#" onclick="if(display(1))document.getElementById('test1').submit();return false;"> Click</a>

That way it works without changing the PHP code.

Guffa
Doesn't work with Lynx. =]
strager
Neither do I. ;)
Guffa
I don't think any javascript works with Lynx.
Calvin