SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JAVASCRIPT submit and PHP variable

    Hello, I am trying to pass a php variable through a javascript submit form button:

    <a href="javascript:document.theForm.submit();"><img src="image.gif" alt="Submit" /></a>

    I can use hidden fields however I don't want to because I might have several of these buttons in one single form and I want to get the variable depending on the button.

    What I want is something like being able to add ?var=xxx to the end of the javascript javascript:document.theForm.submit()?var=xxx; so I can know what button was clicked.

    Any thoughts? thanks in advance

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The submit() function submits the form to the URI that was specified in the required action attribute in the <form> tag. So you should include the query parameter in the action attribute.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using javascript you mean? Can you post an example? thanks

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it's a PHP variable it must be known when you create the page, so you could put the query parameter in the action attribute with PHP.

    Code PHP:
    echo '<form action="myscript.php?var=', htmlspecialchars($myVar), '" method="post">';

    If you're using HTTP GET, rather than POST, you can just put the value in a hidden form field:
    Code HTML4Strict:
    <input type="hidden" name="var" value="xxx">
    Birnam wood is come to Dunsinane


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •