I am trying to post data using jquery for insertion but not able to do so the variables are geting values correctly but i dont think so that php page is working after that, i dont think that there is any mistake in php page. Here is my code
<script>
function print_this(){
var nit =$('#a').val();
var reg = $('#b').val();
var c = $('#csa').val();
var d = $('#dc').val();
var e = $('#Ee').val();
var f = $('#Er').val();
var w = $('#w').val();
var h = $('#w').val();
var i = $('#i').val();
PostValue(a,b,c,d,e,f,w,h,i)}
function PostValue(a,b,c,d,e,f,w,h,i){
$.ajax({
type: "POST",
url: "print.php",
data: {a:a,b:b,c:c,d:d,e:e,f:f,w:w,h:h,i:i},
success:function(data){
$("#print").printThis();
}
})
}
Here is my php page
$conn = mysql_connect('localhost','','') or die ("Connection Problem");
mysql_select_db("") or die ("Database Problem");
function print_this(){
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$query = "select * from print where a = $a and b = $b and c = $c and d = $d";
$result = mysql_query($query);
if(mysql_num_rows($result) == 0)
{
$query = "INSERT INTO print (id ,a,b,c,d,e,f,w,h,i) VAUES
(NULL, '".$_POST['a']."','".$_POST['b']."','".$_POST['c']."','".$_POST['d']."','".$_POST['e']."','".$_POST['f']."','".$_POST['w']."','".$_POST['h']."','".$_POST['i']."')";
$result = mysql_query($query) or die();}
You need to set the content type on your ajax post and stringify your JSON.
$.ajax({
type: "POST",
url: "print.php",
contentType: "application/json",
data: JSON.stringify({a:a,b:b,c:c,d:d,e:e,f:f,w:w,h:h,i:i}),
success: function (data) {
$("#print").printThis();
}
})
Be sure to include JSON.js if you are supporting older browsers.
I notice that when you insert data to your table, you considered your data as a varchar but when you want to select you didn't put your values between single quotes, for more details add after the first mysql_query(), mysql_error() function to debug your script.