简体   繁体   English

使用ajax从php发送和返回值时,为什么会得到javascript和html代码?

[英]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.

相关问题 当我尝试使用ajax post将值从JavaScript发送到PHP时获取Error值 - Getting Error value when I try to send value from JavaScript to PHP with ajax post 当我尝试使用ajax post将值从JavaScript发送到PHP时,URL未加载 - URL is not getting loaded when I try to send value from JavaScript to PHP with ajax post 使用ajax将参数传递给onClick事件的函数时,我是否正在获取html和javascript代码(同一页面上的Ajaxing) - Am i getting html and javascript code when using ajax to pass parameters to the function on onClick event (Ajaxing on the same page) 为什么这个 JS 和 PHP 代码不起作用? (我正在尝试将值从 JS 发送到 PHP) - Why doesn't this JS & PHP code work? (I am trying to send a value from JS to PHP) 为什么我从jquery ajax调用中获取HTML响应 - Why am I getting HTML response from jquery ajax call 为什么我在浏览器地址栏调用中从PHP WEB服务获得空返回值? - Why am I getting an empty return value from PHP WEB Service that works in a browser address bar call? 当我使用sencha touch通过ajax请求将textfield值发送到php时出现错误 - getting error when i send the textfield value through ajax request to php using sencha touch 当我在代码中单击添加到购物车时,我得到的值相同。 这是PHP中的ajax响应 - I am getting the same value when I click on add to cart in my code. It's an ajax response in php 通过HTML / PHP中的按钮使用Ajax发送POST值 - Send POST value using Ajax from a button in HTML/PHP 为什么我在 php 中发送电子邮件时会收到 html 代码? - Why I get html code when I send email in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM