使用jQuery AJAX提交表单 [英] Submitting Form using jquery AJAX
本文介绍了使用jQuery AJAX提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery ajax提交表单,但是我的数据没有发布到PHP,它在 $ _ POST
数组中不返回空数组.
I am trying to submit my form using jQuery ajax, but my data isn't posting to PHP it returns empty array nothing in $_POST
array.
这是我的代码-这是我的表格:
This is my code - here is my form:
<form action = "/webdevelopmentpakistan/send_mail.php" method = "post" class = "myForm" >
<div class="row">
<div class="col-md-3 col-sm-12">
<div class="form-group">
<input class="form-control" id="fname" type="text" required name= "full_name" placeholder="Full Name"
/>
</div>
</div>
<div class="col-md-3 col-sm-12">
<div class="form-group">
<input class="form-control" type="tel" required name = "phone" placeholder="+92" id="phone" onkeypress="return isNumberKey(event)" />
</div>
</div>
<div class="col-md-3 col-sm-12">
<div class="form-group">
<input class="form-control" type="email" required name = "email" id="email" placeholder="Email"/>
</div>
</div>
<div class="col-md-3 col-sm-12">
<div class="form-group">
<input class="btn popup" type="submit" name = "submit" value="CONTACT OUR CONSULTANT"/>
</div>
</div>
</div>
</form>
其为ajax部分:
$('form').on('submit', function (e) {
e.preventDefault();
var url = $(this).attr("action");
var form_data = $(this).serialize();
$.ajax({
type: 'POST',
url: url,
data: $('.myForm').serialize() ,
dataType : 'JSON',
//contentType: "application/x-www-form-urlencoded",
success: function (data) { // here I'm adding data as a parameter which stores the response
console.log(data); // instead of alert I'm changing this to console.log which logs all the response in console.
},
error:function(xhr, textStatus, thrownError, data)
{
console.log("Error: " + thrownError);
console.log("Error: " + textStatus);
}
});
// var popup = document.getElementById("myPopup");
// popup.classList.toggle("show");
console.log(form_data);
});
在其他页面上使用的PHP代码:
PHP CODE using at other page:
if(isset($_POST)) {
echo json_encode($_POST);
}
这是我的序列化数组,正在提交表单,但没有传递给php
and this is my serialize array which I am getting on submission of form but it isn't getting passed to php
full_name=talha&phone=012345678&email=admin%40gmail.com
推荐答案
欢迎使用stackoverflow,这是所做的更改,希望它能起作用
welcome to stackoverflow, here are the changes, hope it will works
$.ajax({
type: 'POST',
url: url,
data: $('.myForm').serialize() ,
dataType : 'json', // changing data type to json
success: function (data) { // here I'm adding data as a parameter which stores the response
console.log(data); // instead of alert I'm changing this to console.log which logs all the response in console.
}
});
在php
if(isset($_POST)) {
echo json_encode($_POST);
}
这应该在您的控制台中打印发布参数数组,但是您将在php中获得一个数组.
this should print array of post parameters in your console, however you will get an array in php.
这篇关于使用jQuery AJAX提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文