[英]Simple call to php script using AJAX not working
I'm new to javascript and AJAX. 我是javascript和AJAX的新手。 I have dynamic HTML table to which I add a new column with a textarea.
我有动态HTML表,在其中添加了带有textarea的新列。 I create a javascript array storing the name of all the textarea which I wish to pass to my php script.
我创建了一个javascript数组,存储了我希望传递给我的php脚本的所有textarea的名称。
Here's my javascript function: 这是我的JavaScript函数:
function checkout()
{
$.ajax({
type : "POST",
url : "loadmsg.php",
data : {'file_array' : upload},
success : function(data)
{
if(data.status == 'success')
alert("Thank you for subscribing!");
else if(data.status == 'error')
alert("Error on query!");
}
});
}
Here upload is a global javascript array that I wish to pass to my php script loadmsg.php. 这里上传的是一个全局javascript数组,我希望传递给我的php脚本loadmsg.php。
Here's the loadmsg.php file: 这是loadmsg.php文件:
<?php
if(isset($_POST['file_array']))
{
$file_array = $_POST['file_array'];
echo "<script type='text/javascript'>alert('Success');</script>";
}
?>
But when the checkout function is executed there's no alert box. 但是,当执行checkout功能时,没有警报框。 I have checked that the upload array is not empty.
我检查了上传数组是否为空。
Can anyone tell me where I'm going wrong? 谁能告诉我我要去哪里错了?
After debugging using Firebug I get the following error in console 使用Firebug调试后,我在控制台中收到以下错误
ReferenceError:$ not defined
on the $.ajax
line ReferenceError:$ not defined
在$.ajax
行上ReferenceError:$ not defined
change your php code like this 这样改变你的PHP代码
<?php
if(isset($_POST['file_array']))
{
$file_array = $_POST['file_array'];
echo json_encode(array('status' => 'success'));
}
?>
and some change to your js code 对您的js代码进行一些更改
$.ajax({
type : "POST",
url : "loadmsg.php",
data : {'file_array' : 'upload'},
success : function(data)
{
var response = $.parseJson(data);
if(response.status == 'success')
alert("Thank you for subscribing!");
else if(response.status == 'error')
alert("Error on query!");
}
});
Hope it will works 希望它能起作用
Try this it's working for me: 试试这个对我有用:
your script 你的剧本
function checkout()
{
$.ajax({
type : "POST",
url : "form1.php",
data : {'file_array' : upload},
success : function(data)
{
var data = JSON.parse(data);
if(data.status == 'success')
alert("Thank you for subscribing!");
else if(data.status == 'error')
alert("Error on query!");
}
});
}
your PHP should be., 您的PHP应该是。,
<?php
if(isset($_POST['file_array']))
{
$file_array = $_POST['file_array'];
$data['status'] = 'success';
echo json_encode($data);
}
?>
You need to echo variable from backend of AJAX (PHP) file. 您需要从AJAX(PHP)文件的后端回显变量。
Alert is already there in Javscript file. Javscript文件中已经存在警报。
So, the corrected PHP file is: 因此,更正后的PHP文件是:
<?php
if(isset($_POST['file_array']))
{
$file_array = $_POST['file_array'];
echo 'success';
}
?>
AND js, 和js
success : function(data)
{
if(data == 'success')
alert("Thank you for subscribing!");
else if(data == 'error')
alert("Error on query!");
}
You can use Firefox's Firebug to debug this. 您可以使用Firefox的Firebug进行调试。
Go to Firebug's
Console
tab and see which requests are going. 转到
Firebug's
Console
标签,然后查看正在进行的请求。
It will show: 它会显示:
Parameter being posted 正在发布参数
Output from backend file. 后端文件的输出。
From here, you will get exact idea of data flow. 从这里,您将确切了解数据流。
include in your head tag 包括在您的头部标签中
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
javascript JavaScript的
don't forget to add dataType: "json",
to your ajax
不要忘记将
dataType: "json",
添加到您的ajax
function checkout()
{
$.ajax({
type : "POST",
dataType: "json",
url : "loadmsg.php",
data : {'file_array' : upload},
success : function(data)
{
if(data.status == 'success')
alert("Thank you for subscribing!");
else if(data.status == 'error')
alert("Error on query!");
}
});
}
php PHP
<?php
if(isset($_POST['file_array']))
{
$file_array = $_POST['file_array'];
$arr['status']='success';
echo json_encode($arr);
}
?>
Try this 尝试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.