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?

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


    
    $(document).ready(function(){
        $("#product").submit(function(){
            $('input[name=form_id]').val($('.button_class').val());
              $.ajax({  
                type: "POST",  
                url: "access.php",  
                data: $("#product").serialize(),
                dataType: "json", 
                success: function(msg){ 
                    $('#formResponse').html(msg.message);
      
                },  
                error: function(){  
                    $('#formResponse').text("There was an error submitting the form. Please try again.");  
                }  
        });  
      
            //make sure the form doesn't post  
            return false;  
      
        });  
      
    });   
    
    
    Code (markup):
     
    michaelk46, May 11, 2012 IP
  2. yho_o

    yho_o Well-Known Member

    Messages:
    353
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    #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

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

    
    $(function(){
        $("#button1").click( function(){
            // do something when first button is clicked
            // maybe your ajax request here
            return false; // Important!
        });
        $("#button2").click( function(){
            // do something when first button is clicked
            // maybe your ajax request here
            return false; // Important!
        });
    });
    
    Code (markup):
     
    yho_o, May 18, 2012 IP