[英]Why am i getting javascript and html code when using ajax to send and return value from php?
Today I tried Ajax with the below code. 今天,我使用以下代码尝试了Ajax。 Logic seems to be correct because success function is triggering but i can't able to get return value from php file, instead i'm getting Javascript and html code when i alert the response.
逻辑似乎是正确的,因为成功函数正在触发,但是我无法从php文件获取返回值,相反,当我警告响应时,我正在获取Javascript和html代码。
example1.php example1.php
<?php
echo $_POST['name'];
?>
<form action="?" method="post">
<input type="text" id="d1" name="name" value="">
<input type="text" id="d2" name="place" value="">
<input id="target" type="submit" name="submit" value="send">
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#target").click(function()
{
var data1=$("#d1").val();
var data2=$("#d2").val();
$.ajax
({
type:"POST",
url:"example1.php",
data:{name:data1,place:data2},
success:function(msg)
{
alert(msg);
}
});
return false;
});
});
</script>
When i run the example1.php it shows the alert message like given below Submitted name 当我运行example1.php时,它显示警告消息,如下所示。
<form action="?" method="post">
<input type="text" id="d1" name="name" value="">
<input type="text" id="d2" name="place" value="">
<input id="target" type="submit" name="submit" value="send">
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#target").click(function()
{
var data1=$("#d1").val();
var data2=$("#d2").val();
$.ajax
({
type:"POST",
url:"arithmetic.php",
data:{name:data1,place:data2},
success:function(msg)
{
alert(msg);
}
});
return false;
});
});
</script>
Why am i getting this instead of name and place value from example1.php? 为什么我从example1.php而不是名称和位置值中获取此信息?
UPDATE considering that the question changed 考虑到问题已更改的更新
You're posting to the same file where the form is, so your response includes the HTML code in that page. 您将发布到表单所在的文件,因此您的响应将在该页面中包含HTML代码。 You have to post to a different PHP file that only responds with the content you want, or change your current file to respond differently if some data was posted.
您必须发布到另一个仅响应所需内容的PHP文件,或者将当前文件更改为在发布某些数据时做出不同响应。
Your ajax request is not going to response.php
, because you told jQuery to post to a different URL: 您的ajax请求不会发送到
response.php
,因为您告诉jQuery发布到其他URL:
url:"arithmetic.php",
Change that to response.php. 将其更改为response.php。
To guarantee you always post with ajax, you should bind to the form's submit
event, instead of the submit button click: 为了确保始终使用ajax进行发布,您应该绑定到表单的
submit
事件,而不是单击Submit按钮:
$('form').submit(function(e) {
// ajax stuff
// prevent default (submit)
return false;
});
With your current code, the form will submit without ajax if you hit enter while focused on one of those input fields. 使用当前代码,如果您将注意力集中在这些输入字段之一上,则按Enter即可提交不带ajax的表单。
Change your response code as follows. 如下更改您的响应代码。 You need to stop execution after writing your response.
编写响应后,您需要停止执行。
<?php
if(isset($_POST['name'])) {
echo $_POST['name'];
echo $_POST['place'];
exit(0);
}
?>
chage the url to url:"response.php"
将网址更改为
url:"response.php"
and write the code in responce.php like this 并像这样在responce.php中编写代码
$name=$_POST["name"];
$place=$_POST["place"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.