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