如何在php中使用ajax发布表单?

如何在php中使用ajax发布表单?,ajax,forms,ajaxform,Ajax,Forms,Ajaxform,我正在尝试使用ajax提交表单,但我设置了它,当它成功时,jquery警报应该显示,但它没有显示,所以我认为它不成功。你们知道为什么吗 这是我试图用ajax提交的表单 <b>Product Title: </b><br><input style="width: 50%;" class="form-control itemtitle" type="text" name="title" required="true"><br> <b&

我正在尝试使用ajax提交表单,但我设置了它,当它成功时,jquery警报应该显示,但它没有显示,所以我认为它不成功。你们知道为什么吗

这是我试图用ajax提交的表单

<b>Product Title: </b><br><input style="width: 50%;" class="form-control itemtitle" type="text" name="title" required="true"><br>
<b>Product Description: </b><br><input style="width: 50%;" class="form-control itemdescription" type="text" name="description" required="true"><br>
<b>Product Price: </b><br><input style="width: 50%;" class="form-control itemprice" type="number" name="price" required="true"><br>
<b>Product Image Address: </b><br><input style="width: 50%;" class="form-control itemimageaddress" type="text" name="image" required="true"><br>
<button type="submit" class="btn btn-primary createproductsubmit">Create Product</button>

对代码的一些注释

1您应该用标记包围表单输入和按钮元素。 2.不通过POST请求传递字符串,只需传递如下对象:

data: {
    itemtitle: itemtitle,
    itemdescription: itemdescription,
    itemprice: itemprice,
    itemimageaddress: itemimageaddress
}
3警报不是jquery函数,而是纯javascript函数。我建议您使用console.log并在控制台中查看输出

4检查控制台是否存在可能的javascript错误。如果出现js错误,代码甚至不会触发ajax调用

5检查“开发工具网络”选项卡,查看是否正在发送ajax请求。检查它是否到达正确的目的地,并检查服务器响应是什么

检查所有这些,你应该走在正确的道路上。

只需使用

$.post("pages/newproductaction.php", {
itemtitle: itemtitle,
itemdescription: itemdescription,
itemprice: itemprice,
itemimageaddress: itemimageaddress
}, function(data){

});
$.post("pages/newproductaction.php", {
itemtitle: itemtitle,
itemdescription: itemdescription,
itemprice: itemprice,
itemimageaddress: itemimageaddress
}, function(data){

});