1

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

6
  • Where are you getting this line from: data : {'file_array' : upload}? Where have you defined upload? Commented Jul 27, 2015 at 4:17
  • Which alert you need to show? Commented Jul 27, 2015 at 4:17
  • what is your expected message? Commented Jul 27, 2015 at 4:18
  • @V P I added the alert in php script for debugging. I basically wish to load the upload array in php variable $file_array Commented Jul 27, 2015 at 4:21
  • @Rasclatt upload is a global javascript array that I defined and initialized in the same file before the checkout function Commented Jul 27, 2015 at 4:23

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

I still don't get any message
@Hossain still no alert
Try echo json_encode(array('status' => 'success')); not return
@hossain sorry for typo mistake
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
if(isset($_POST['file_array']))
{
    $file_array = $_POST['file_array'];
    $data['status'] = 'success';

    echo json_encode($data);
}
?>

Comments

0

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

5 Comments

you want to echo "<script type='text/javascript'>alert('Success');</script>"; in your web?.
did ur alert("Thank you for subscribing!"); worked using this code?
No the alert("Thank you for subscribing!) is not working. I wish to pass the upload array in my php variable
I just checked I'm getting an error ReferenceError:$ not defined on the $.ajax line
hav u included <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> in your HTML <head> tag??
0

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.

2 Comments

I did that but I still don't get any message
I just checked I'm getting an error ReferenceError:$ not defined on the $.ajax line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.