I keep getting the error message 'Syntax Error: unexpected token <' and a xhr.status of 200 if I run the following ajax request. Considering that I am able to email myself the variable passed from the JavaScript file to the php file it seems like that the problem occurs when the data is passed back using json to the JavaScript file.
I am relativeley new to ajax and unfortunately I am unable to solve this issue on my own since hours.
JavaScript code:
var discount ="";
$('#apply_discount').click(function(){
discount_input = $('#discount_input').val();
$.ajax({
type: 'POST',
url: 'storescripts/discount.php',
data: {
discount_input: discount_input,
},
cache: false,
dataType: 'json',
success: function(data){
discount = data.discount;
alert(discount);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
});
php code:
<?php
include("includes/session_start.php");
include("connect_to_mysql.php");
$discount="";
if (isset($_POST['discount_input'])) {
$discount_input = $_POST['discount_input'];
$discount_input = stripslashes($discount_input);
$discount_input = preg_replace('/\s+/', '', $discount_input);
$discount = mysql_query("SELECT discount FROM discount WHERE coupon_name = '$discount_input' LIMIT 1")or die(mysql_error());
$discount = mysql_fetch_row($discount);
$discount = implode('', $discount);
mail("my@myemail",$discount,"some message","From: test@mysite.com");
echo json_encode(array('discount' => $discount));
}
?>