html – How can I convert my button into a hyperlink in PHP? – Education Career Blog

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?

,

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.

,

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.

,

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.

Leave a Comment