开发者

jQuery submit ajax form with 2 submit buttons

im trying to achieve the following, in php i have a form like this:

<form method="post"  id="form_1" action="php.php">
<input type="submit" value="add" name="sub"/>
<input type="submit" value="envoi" name="sub"/>
</form>

the form action file is:

<?php

if( $_POST["sub"]=="add"){  ?>

 <script>
 alert("")
 </script>
<?php  echo "ZZZZZZ";   ?>

<?php } ?>

so this means if i press sub with value add an alert prompt will come up, how can i do the same thing(differentiate both submit) but using a Ajax request:

the following code so does not work:

 $(function(){
      $('form#form_1').submit(function(){
var _data= $(this).serialize()开发者_运维问答
$.ajax({
        type: 'POST',
        url: "php.php?",
        data:_data,
        success: function(html){
         $('div#1').html(html)

          }
     })
})
  })
  </script>
</head>

<body>
<div id="1" style="width: 100px;height: 100px;border: 1px solid red"></div>

<form method="post"  id="form_1" action="javascript:;">
<input type="submit" value="add" name="sub"/>
<input type="submit" value="envoi" name="sub"/>
</form>
</body>


You could put the event handler on the buttons instead of on the form. Get the parameters from the form, and then add a parameter for the button, and post the form. Make sure the handler returns "false".

$(function() {
  $('input[name=sub]').click(function(){
    var _data= $('#form_1').serialize() + '&sub=' + $(this).val();
    $.ajax({
      type: 'POST',
      url: "php.php?",
      data:_data,
      success: function(html){
         $('div#1').html(html);
      }
    });
    return false;
  });
});

You have to explicitly add the "sub" parameter because jQuery doesn't include those when you call "serialize()".


In this case you need to manually add the submit button to the posted data, like this:

$(function(){
  $('form#form_1 :submit').submit(function(){
    var _data = $(this).closest('form').serializeArray(); //serialize form
    _data.push({ name : this.name, value: this.value });  //add this name/value
    _data = $.param(_data);                               //convert to string
    $.ajax({
      type: 'POST',
      url: "php.php?",
      data: _data,
      success: function(html){
        $('div#1').html(html);
      }
    });
    return false; //prevent default submit
  });
});

We're using .serializeArray() to get a serialized version of the form (which is what .serialize() uses internally), adding our name/value pair to that array before it gets serialized to a string via $.param().

The last addition is a return false to prevent the default submit behavior which would leave the page.


Lots of semicolon missing, see below

 $(function(){
      $('form#form_1').submit(function(){
         var _data= $(this).serialize();
         $.ajax({
            type: 'POST',
            url: "php.php?",
            data:_data,
            success: function(html){
               $('div#1').html(html);    
            }
         });
      });
  });


jQuery Form plugin provide some advance functionalities and it has automated some tasks which we have to do manually, please have a look at it. Also it provides better way of handling form elements, serialization and you can plug pre processing functions before submitting the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜