Function Call from Hyperlink Onclick (with parameter)

Discussion in 'PHP' started by zenoxman, Apr 12, 2008.

  1. #1
    I want to call a function in a separate .php file when I click on a hyperlink (href is NOT php file). My .htm code looks something like this:

    (I'm testing the code with <h1> or else I won't see the server's response.)

    <html>
    <body>
    <form name="coolform" method="get" action="test.php?q=PRE_SET_PARAM">
    <h1 onclick="document.coolform.submit()">
    Random Destination
    </h1>
    </form>
    </body>
    </html>

    And test.php looks like this:

    <html>
    <body>
    <?php
    echo $_GET['q'];
    ?>
    </body>
    </html>

    My only guess as to why this doesn't work is that it looks for test.php?q=PRE_SET_PARAM instead of test.php because ?q= can only be added at submission.

    How can I pass variable q to test.php
     
    zenoxman, Apr 12, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    That should work, but you could also try using a hidden named q.

    
    <html>
    <body>
    <form name="coolform" method="get" action="test.php">
    <input type="hidden" name="q" value="PRE_SET_PARAM" />
    <h1 onclick="document.coolform.submit()">
    Random Destination
    </h1>
    </form>
    </body>
    </html>
    
    HTML:
    Does the form currently submit at all. I didn't think that an h1 onclick is supported by every browser. You may need to format a submit button to look text if it isn't well supported.
     
    jestep, Apr 13, 2008 IP