Simple call to php script using AJAX not working

Lakshya Kejriwal

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

Vishnu Prasad

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Simple call to php script using AJAX not working

From Dev

Using Ajax and JQuery to call PHP Script

From Dev

Calling a php script using ajax not working

From Dev

Simple PHP AJAX script

From Dev

Call php script with ajax

From Dev

CSS, Java script not working properly after ajax call in php

From Dev

CSS, Java script not working properly after ajax call in php

From Dev

Using $.post, $.ajax or AJAX properly in CodeIgniter to call a PHP script

From Dev

Call a simple PHP function with ajax

From Dev

Why is this simple php script not working?

From Dev

How to do jQuery autocomplete using an AJAX call to a PHP script?

From Dev

Running Python script from PHP by using an AJAX call

From Dev

AJAX call to a PHP file not working

From Dev

Using ajax to call php

From Dev

How to execute a PHP email script using AJAX? [Current code is not working]

From Dev

call script tag by using ajax

From Dev

AJAX call not working - PHP, MySQL, jQuery/Ajax

From Dev

php/Wordpress/Ajax Button call a php script

From Dev

Simple Login using ajax in php

From Dev

jQuery AJAX simple PHP Post not working

From Dev

Can't make a simple php - Ajax working

From Dev

Retrieving php variables from ajax call but not working

From Dev

JavaScript Ajax call to PHP function not working

From Dev

Livesearch using php and ajax not working

From Dev

call php script on click of button through ajax

From Dev

jQuery AJAX Call to PHP Script with String Return

From Dev

jQuery AJAX Call to PHP Script with JSON Return

From Dev

AJAX call the PHP script not outputting any results

From Dev

Passing result from AJAX call to PHP script

Related Related

HotTag

Archive