Here is my code.I am pretty sure my code is right but it is not functioning properly just for the last query.All the remaining queries are working well and fine but the last query is not returning value back with some object which is not accessible however the updation of table is occurring . What is wrong? I am attaching the pic of the error being displayed.Error Image
First the code for frontend page :
<!DOCTYPE html>
<head>
</head>
<body>
<form>
<input type="text" id='dat'>
<button id='submit'>Submit</button>
</form>
<script src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.3.js">
</script>
<script>
$(function(){
$('#submit').click(function(){
var x=$('#dat').val();
$.ajax({
type:"GET",
url: "promo_code_apply2.php",
dataType: "json",
data: { x : x },
success: function(result){
alert(result);
},
error: function(e) {
// Handle error here
console.log(e);
}
});
});
});
</script>
</body>
Now the code for backend :
<?php
include('connection.php');
include('functions.php');
$p_code=$_GET['x'];
$sql="select * invite_codes where promo_name='".$p_code."'";
$res=mysqli_query($con,$sql);
if(!$res)
{
$data=mysqli_error($con);
echo json_encode($data);
die();
}
else{
if(mysqli_num_rows($res)==0)
{
$data="Invalid Code";
echo json_encode($data);
}
else{
$row=mysqli_fetch_assoc($res);
$ph=$row['user_mob'];
$sql2="update user_signup set no_of_referrals=no_of_referrals+1 where promo_name='".$p_code."'";
$res2=mysqli_query($con,$sql2);
if(!$res2)
{
$data=mysqli_error($con);
echo json_encode($data);
die();
}
else
{
$data="Conngrats";
echo json_encode($data);
}
}
}
?>
You didn't set your button type and as it is inside the form its default type is submit. When your button is pushed your jQuery event handler is called, but after its completion the default action is called which is submit form action. It reloads your page and your jQuery handler wont get data back from PHP, because JavaScript state resets after reload.
In order to prevent this behaviour you can either:
Put return false at the end of your jQuery event handler - like this:
$(function(){ $('#submit1').click(function(){ ... }); return false; // <--- prevents default action }); });
or
type="button"