1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

multiple button issue

Discussion in 'jQuery' started by michaelk46, May 11, 2012.

  1. #1
    I am having a bit of an issue with a form that has multiple submission buttons on it. What I want it to do is when the form is submitted, it will read the value of the button that was clicked and then put that value into a hidden field.

    What it is currently doing is when either search button is hit it reads the first submission button listed in the document and not necessarily the button that was clicked. Anyone know how to modify it so it actually reads the button that is hit instead of the first one listed?

    HTML:
    1.  
    2. <input name="submitForm" type="submit" class="button_class" value="Search 1">
    3. <input name="submitForm" type="submit" class="button_class" value="Search 2">
    4.  


    Code (Text):
    1.  
    2. $(document).ready(function(){
    3.     $("#product").submit(function(){
    4.         $('input[name=form_id]').val($('.button_class').val());
    5.           $.ajax({  
    6.             type: "POST",  
    7.             url: "access.php",  
    8.             data: $("#product").serialize(),
    9.             dataType: "json",
    10.             success: function(msg){
    11.                 $('#formResponse').html(msg.message);
    12.  
    13.             },  
    14.             error: function(){  
    15.                 $('#formResponse').text("There was an error submitting the form. Please try again.");  
    16.             }  
    17.     });  
    18.  
    19.         //make sure the form doesn't post  
    20.         return false;  
    21.  
    22.     });  
    23.  
    24. });  
    25.  
    26.  
     
    michaelk46, May 11, 2012 IP
  2. yho_o

    yho_o Member

    Messages:
    286
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    45
    #2
    you can't add 2 submit buttons for a form, but you may do something like this if you going to make it with ajax requests and return false when buttons clicked

    Code (Text):
    1.  
    2. <input name="submitForm" type="submit" class="button_class" value="Search 1" [COLOR=#ff0000]id="button1"[/COLOR]>
    3. <input name="submitForm" type="submit" class="button_class" value="Search 2" [COLOR=#ff0000]id="button2"[/COLOR]>
    4.  
    jQuery:

    Code (Text):
    1.  
    2. $(function(){
    3.     $("#button1").click( function(){
    4.         // do something when first button is clicked
    5.         // maybe your ajax request here
    6.         return false; // Important!
    7.     });
    8.     $("#button2").click( function(){
    9.         // do something when first button is clicked
    10.         // maybe your ajax request here
    11.         return false; // Important!
    12.     });
    13. });
    14.  
     
    yho_o, May 18, 2012 IP