Narendra Verma Narendra Verma - 28 days ago 8
Ajax Question

Not defined error in console while passing php value to ajax

I am passing PHP value to AJAX but I am getting an error that variable is not defined.

$value_r is not defined


When the user selects the checkbox then the value of $value_r will auto display.

AJAX

$(document).ready(function () {
$("input[name=value_r]").click(function(){
$.ajax({
type: "POST",
url: "includes/compare.php", //
data: {id: <?php echo '$value_r'?>},
success: function(msg){
alert(msg);
},
error: function(){
alert("failure");
}
});
});
});


index.php

<input type="checkbox" value="" name="value_r" id="value_r"/>
<label for=""><?php echo $value_r?></label>
<style type="text/css">
input[type=checkbox] + label{
display: none;
}
input[type=checkbox]:checked + label{
display: inline-block;
}
</style>

Roy Roy
Answer Source

You are getting the error here.
Change

data: {id: <?php echo '$value_r'?>},

to

data: {id: '<?php echo $value_r ?>'},

Regards!