SitePoint Sponsor

User Tag List

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

    Smile PHP variable and JAVASCRIPT submit

    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 Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create a hidden field and fill that before the call to submit.
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also skip the javascript and use
    Code:
    <input type="image" src="..." name="a">
    <input type="image" src="..." name="b">
    PHP Code:
    // different browsers send the name for type=image differently...
    // some send the name as is, some only send the x-y coordinate modified name
    // eg the browser sends a_x as the x coordinate of the click, a_y as the y coord...
    $a = isset($_POST['a']) || isset($_POST['a_x']);
    $b = isset($_POST['b']) || isset($_POST['b_x']); 
    Don't forget the user can submit the form by pressing enter instead of clicking a button.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create a hidden field and fill that before the call to submit.
    How can I do this? thanks

  5. #5
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by raml View Post
    How can I do this? thanks
    Code:
    document.getElementById('someForm').someHiddenInput.value = "whatever";
    document.getElementById('someForm').submit();


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
  •