Thursday, April 28, 2011

How can I convert my button into a hyperlink in PHP?

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?

From stackoverflow
  • 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.

    Guffa : The "inverted commas" are apostrophes.
    Rich Bradshaw : Ah - that's the word I was thinking of - wouldn't come to mind for some reason!
    Calvin : Actually, inverted commas is just as valid: http://en.wikipedia.org/wiki/Single_quotes And in programming, they're usually referred to as 'single quotes' rather than apostrophes/inverted commas.
  • 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.
    Pim Jager : He should not use & unless he wants the action var to hold: show&id=1. If he wants action=show and id=1 then he should realy use the & and not &
    strager : @Jager, & will be translated into & in the URL. & is used to escape the & in the XML.
  • 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.

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

0 comments:

Post a Comment