如何获取php页面以接收来自html页面的ajax帖子 [英] How to get php page to receive ajax post from html page
问题描述
我有一个非常简单的表单,其中有一个用于输入名字的字段.我捕获了表单数据,并使用标准的jQuery发布方法通过ajax将其传输到PHP页面.但是,我根本无法从PHP页面获得任何在服务器端捕获任何数据的响应.我不确定自己做错了什么或缺少什么.
这是我的代码.
表格:
<form action="process.php" method="POST">
<div class="form-group">
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="firstName">First name</label>
<input type="text" class="form-control" name="firstName" id="firstName" placeholder="First name">
<div class="d-none" id="firstName_feedback">
<p>Please enter a first name.</p>
</div>
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>
这是我的Jquery Ajax调用:
<script>
$(document).ready(function() {
$('form').submit(function(event) {
var formData = $("form").serialize();
console.log(formData);
$.ajax({
type: 'POST',
url: 'form.php',
data: formData,
dataType: 'json',
encode: true
})
.done(function(data) {
console.log(data);
});
event.preventDefault();
});
});
</script>
这是我的PHP页面:
if(isset($_POST['formData']))
$ajaxData = ($_POST['formData']);
echo $ajaxData;
{
}
在Ajax函数中,您将formData
的内容传递给服务器,尽管不是作为formData
而是作为其原始输入名称./p>
在这种情况下,您有:
<input type="text" class="form-control" name="firstName" id="firstName" placeholder="First name">
输入的名称为firstName,因此您需要调用$_POST['firstName']
而不是$_POST['formData']
.
if (isset($_POST['firstName'])) {
$ajaxData = $_POST['firstName'];
echo $ajaxData;
}
这同样适用于您在表单中拥有的任何其他字段,因此,例如,使用名称为lastName
的另一个输入意味着您必须调用$_POST['lastName']
才能访问它.
我上面容纳的PHP代码中还有一些放错了括号和括号的地方.
I have a very simple form that has an input field for first name. I captured the form data and transmitted it via ajax to a PHP page using the standard jQuery posting method. However, I am not able at all get any responses from the PHP page that any data was captured on the server-side. I am not sure what I have done wrong or what is missing.
Here is my code.
Form:
<form action="process.php" method="POST">
<div class="form-group">
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="firstName">First name</label>
<input type="text" class="form-control" name="firstName" id="firstName" placeholder="First name">
<div class="d-none" id="firstName_feedback">
<p>Please enter a first name.</p>
</div>
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>
Here is my Jquery Ajax call:
<script>
$(document).ready(function() {
$('form').submit(function(event) {
var formData = $("form").serialize();
console.log(formData);
$.ajax({
type: 'POST',
url: 'form.php',
data: formData,
dataType: 'json',
encode: true
})
.done(function(data) {
console.log(data);
});
event.preventDefault();
});
});
</script>
And here is my PHP page:
if(isset($_POST['formData']))
$ajaxData = ($_POST['formData']);
echo $ajaxData;
{
}
In your Ajax function, you're passing the contents of formData
to the server, though not as formData
but as their original input name.
In this case, you have:
<input type="text" class="form-control" name="firstName" id="firstName" placeholder="First name">
The input's name is firstName, so you need to call $_POST['firstName']
instead of $_POST['formData']
.
if (isset($_POST['firstName'])) {
$ajaxData = $_POST['firstName'];
echo $ajaxData;
}
The same applies for any other field you would have in your form, so for example, having another input with the name lastName
means you'd have to call $_POST['lastName']
to access it.
There were also some misplaced brackets and parentheses in the PHP code which I accommodated above.
这篇关于如何获取php页面以接收来自html页面的ajax帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!