jQuery提交功能不发送输入提交

时间:2015-02-18 19:10:29

标签: php jquery ajax

我正在寻找我的问题的答案,但我没有找到解决方案。这是类似的主题link但我有不同的代码,我不知道如何从这个主题回答我的代码。

这是我的问题。我将我的表单order_form发送给test.php。 除input submit外,每个表单值都正在发送。我的脚本基于检查<input id="sendform" type="submit" value="ORDER PRODUCT" name="sendform"/>是否已发送。

以下是我用来发送表单的代码。

$("#order_form").submit(function() {

    $.ajax({
           type: "POST",
           url: "includes/test.php",
           data: $("#order_form").serialize(), // serializes the form's elements.
           success: function(data)
           {
                alert(data);
           }
    });

    return false; // avoid to execute the actual submit of the form.
});

这是test.php的内容

echo '<pre>';
print_r($_POST);
echo '</pre>';

这是考试形式,因为我的形式非常大。

  <form id="order_form">
      <input type="text" name="w2b" value="abc"/>
      <input id="sendform" type="submit" value="ZAMAWIAM" name="sendform"/>
  </form>

2 个答案:

答案 0 :(得分:0)

在HTML5表单中,当您将参数和变量传递给ajax函数时,值数据来自&#34;提交输入&#34;没有通过。因此,如果您将表单设置为:

<form>
<input type="text" name="foo" value="bar">
<input type="submit" name="submitTest" value="Hello World!">
</form>

&#34; submitTest&#34;的值没有被发送。唯一被发送的是参数&#34; foo&#34;值&#34; bar&#34;。 &#34; submitTest&#34;在这种情况下,只提交表单并执行ajax调用。

要解决此问题,只需在表单中添加隐藏元素即可。 现在看起来这个

<form>
<input type="text" name="foo" value="bar">
<input type="hidden" name="sendform" value="ZAMAWIAM">
<input type="submit" name="submitTest" value="Hello World!">
</form>

这会将值发送到您的ajax调用,您可以将它用于您可能需要的任何内容。

答案 1 :(得分:0)

确保将jquery代码包装在

$(function(){
//code here 
});

再次不要忘记阻止默认事件,下面是重写你的jquery代码

$(function(){
$("#order_form").submit(function(event) {

event.preventDefault();

    $.ajax({
           type: "POST",
           url: "includes/test.php",
           data: $("#order_form").serialize(), // serializes the form's elements.
           success: function(data)
           {
                alert(data);
           }
    });//end ajax

});//end on submit

});//end jquery
相关问题