简体   繁体   中英

Simple call to php script using AJAX not working

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:

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:

<?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

change your php code like this

<?php
if(isset($_POST['file_array']))
{
    $file_array = $_POST['file_array'];
    echo json_encode(array('status' => 'success'));
}
?>

and some change to your js code

$.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
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.

Alert is already there in Javscript file.

So, the corrected PHP file is:

<?php
if(isset($_POST['file_array']))
{
    $file_array = $_POST['file_array'];
    echo 'success';
}
?>

AND 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.

Go to Firebug's Console tab and see which requests are going.

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

don't forget to add dataType: "json", to your 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
if(isset($_POST['file_array']))
{
    $file_array = $_POST['file_array'];
    $arr['status']='success';
    echo json_encode($arr);
}
?>

Try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM