Reputation: 1388
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
Upvotes: 1
Views: 83
Reputation: 3236
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
Upvotes: 3
Reputation: 727
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
Upvotes: 0
Reputation: 2460
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);
}
?>
Upvotes: 1
Reputation: 23978
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.
Upvotes: 0