Javascript jQuery$.post不适用于特定条件
我正在尝试使用AJAX和jQuery$.post将一些数据插入mySQL数据库。我尝试测试的功能如下 html:代码片段(已经在使用jQuery实现许多其他功能) updateDatabase.phpJavascript jQuery$.post不适用于特定条件,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我正在尝试使用AJAX和jQuery$.post将一些数据插入mySQL数据库。我尝试测试的功能如下 html:代码片段(已经在使用jQuery实现许多其他功能) updateDatabase.php <?php if( $_REQUEST["name"] ) { $name = $_REQUEST['name']; echo "Welcome ". $name; echo "<script>alert('Im here')</script>";
<?php
if( $_REQUEST["name"] ) {
$name = $_REQUEST['name'];
echo "Welcome ". $name;
echo "<script>alert('Im here')</script>";
}
?>
更改此选项
echo "alert('Im here')";
到
你也可以使用
.done(function() {
alert("Success");
})
最终代码
$.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
您正在回显字符串echo“alert('Im here')”代码>。所以
不能将其视为javascript函数
您没有在成功回调中使用响应
您可以做的是:
function(data) {
alert('Success: '+data);
// outputs: Success: Welcome Zara I'm here
}
在php:
echo "Welcome ". $name . " I'm here";
尝试使用json作为返回数据的安全方法
$.post(
"updateDatabase.php",
{ name: "Zara" },
function(data) {//data is all the echoed text from the updateDatabase.php in our case a json string
alert(data.message);
}
);
<?php
if( $_REQUEST["name"] ) {
$name = $_REQUEST['name'];
echo json_encode([message =>"Welcome ". $name])
}
?>
$.post(
“updateDatabase.php”,
{姓名:“Zara”},
函数(data){//data是来自updateDatabase.php的所有回显文本,在本例中是一个json字符串
警报(数据、消息);
}
);
ajax调用与导入不同,updateDatabase.php中的数据以success functionOk的数据变量的形式传递给ajax。现在我明白了。我想我必须使用数据变量来获得输出。你是对的,现在我知道我犯的错误了。对不起这是我的第一篇$0.post。@danimvijay不要担心,当你是新来的时候,它会发生在这里。
echo "Welcome ". $name . " I'm here";
$.post(
"updateDatabase.php",
{ name: "Zara" },
function(data) {//data is all the echoed text from the updateDatabase.php in our case a json string
alert(data.message);
}
);
<?php
if( $_REQUEST["name"] ) {
$name = $_REQUEST['name'];
echo json_encode([message =>"Welcome ". $name])
}
?>