i have problem with jquery submit and post. I would like to submit a form and post it to my php for a check, and return the php result back to the form. However, I have problem with the returning of data, it basically ignores the result.
我有jquery提交和发布的问题。我想提交一个表单并将其发布到我的php进行检查,并将php结果返回给表单。但是,我有数据返回的问题,它基本上忽略了结果。
This is my html form:
这是我的html表单:
<form id="form" method="post">
<p id="status">Status:</p>
<p class="text">
<label for="name" class="label">Name:</label>
<input type="text" id="name" name="name" value="" size="30" />
</p>
<p class="text">
<label for="email" class="label">Email:</label>
<input type="text" id="email" name="email" value="" size="30" />
</p>
<p class="submit">
<input type="submit" name="send_btn" id="send_btn" value="Send" />
</p>
</form>
This is my javascript to do the submit and post:
这是我的javascript做提交和发布:
$('#form').submit(function(e) {
e.preventDefault();
var name = $('#name').val();
var email = $('#email').val();
$.post('notify.php', {name: name, email: email}, function(data) {
$('#status').html(data);
});
});
This is the php that does the check and return the data:
这是检查并返回数据的php:
<?php
if (isset($_POST['name'], $_POST['email']))
{
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
if ($name == "myname")
{
$output = 'It matches!';
}
else
{
$output = 'No matches!";
}
}
?>
Can please highlight what has gone wrong? Thank you.
可以请突出显示出错的地方吗?谢谢。
4 个解决方案
#1
2
You need to echo
or die
in your php script, so your function can get the results.
你需要在php脚本中回显或消亡,这样你的函数才能得到结果。
So, change your script to this:
所以,将脚本更改为:
<?php
if (isset($_POST['name'], $_POST['email']))
{
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
if ($name == "myname")
{
$output = 'It matches!';
}
else
{
$output = 'No matches!';
}
echo $output;
}
?>
Notice the third to last line, where I am calling echo $output
- whatever you echo will be returned from your ajax call. If you want to get more complex, you should return a JSON object.
注意第三行到最后一行,我调用echo $ output - 无论你回应什么,都会从你的ajax调用中返回。如果你想变得更复杂,你应该返回一个JSON对象。
$results = array("result" => "It Matches!", "foo" => "bar");
echo json_encode($results);
EDIT: You also need to change the "
to a '
at the end of your else.
编辑:您还需要在其他人的末尾更改“到a”。
#2
1
Print the answer on PHP as comment suggest echo $output;
在PHP上打印答案作为评论建议echo $ output;
at the end of the line
在行尾
#3
0
In your php code, you aren't actually writing $output to the page.
在您的PHP代码中,您实际上并没有将$ output写入页面。
<?php
// your code
echo $output;
?>
#4
0
dont forget
别忘了
return false;
返回虚假;
If u forget it, the form when submit will refesh and ajax will fail ;
如果你忘了它,提交时的表格将会改写,而ajax将会失败;
$('#form').submit(function(e) {
e.preventDefault();
var name = $('#name').val();
var email = $('#email').val();
$.post('notify.php', {name: name, email: email}, function(data) {
$('#status').html(data);
});
return false;
});
#1
2
You need to echo
or die
in your php script, so your function can get the results.
你需要在php脚本中回显或消亡,这样你的函数才能得到结果。
So, change your script to this:
所以,将脚本更改为:
<?php
if (isset($_POST['name'], $_POST['email']))
{
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
if ($name == "myname")
{
$output = 'It matches!';
}
else
{
$output = 'No matches!';
}
echo $output;
}
?>
Notice the third to last line, where I am calling echo $output
- whatever you echo will be returned from your ajax call. If you want to get more complex, you should return a JSON object.
注意第三行到最后一行,我调用echo $ output - 无论你回应什么,都会从你的ajax调用中返回。如果你想变得更复杂,你应该返回一个JSON对象。
$results = array("result" => "It Matches!", "foo" => "bar");
echo json_encode($results);
EDIT: You also need to change the "
to a '
at the end of your else.
编辑:您还需要在其他人的末尾更改“到a”。
#2
1
Print the answer on PHP as comment suggest echo $output;
在PHP上打印答案作为评论建议echo $ output;
at the end of the line
在行尾
#3
0
In your php code, you aren't actually writing $output to the page.
在您的PHP代码中,您实际上并没有将$ output写入页面。
<?php
// your code
echo $output;
?>
#4
0
dont forget
别忘了
return false;
返回虚假;
If u forget it, the form when submit will refesh and ajax will fail ;
如果你忘了它,提交时的表格将会改写,而ajax将会失败;
$('#form').submit(function(e) {
e.preventDefault();
var name = $('#name').val();
var email = $('#email').val();
$.post('notify.php', {name: name, email: email}, function(data) {
$('#status').html(data);
});
return false;
});