使用AJAX简单调用php脚本无法正常工作

时间:2022-06-30 04:20:32

I'm new to javascript and AJAX. I have dynamic HTML table to which I add a new column with a textarea. I create a javascript array storing the name of all the textarea which I wish to pass to my php script.
Here's my javascript function:

我是javascript和AJAX的新手。我有动态HTML表格,我在其中添加了一个带有textarea的新列。我创建了一个javascript数组,存储了我希望传递给我的php脚本的所有textarea的名称。这是我的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.
Here's the loadmsg.php file:

这里upload是一个全局的javascript数组,我希望传递给我的php脚本loadmsg.php。这是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. 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
ReferenceError:$ not defined on the $.ajax line

但是当执行结账功能时,没有警报框。我检查过上传数组不是空的。谁能告诉我哪里出错了?使用Firebug进行调试后,我在控制台ReferenceError中收到以下错误:$未在$ .ajax行上定义

4 个解决方案

#1


0  

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

尝试这个

#2


3  

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

希望它会奏效

#3


1  

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);
}
?>

#4


0  

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的控制台选项卡,查看要求的请求。

It will show:

它将显示:

  • Parameter being posted

    正在发布的参数

  • Output from backend file.

    后端文件的输出。

From here, you will get exact idea of data flow.

从这里,您将获得数据流的确切概念。

#1


0  

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

尝试这个

#2


3  

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

希望它会奏效

#3


1  

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);
}
?>

#4


0  

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的控制台选项卡,查看要求的请求。

It will show:

它将显示:

  • Parameter being posted

    正在发布的参数

  • Output from backend file.

    后端文件的输出。

From here, you will get exact idea of data flow.

从这里,您将获得数据流的确切概念。