Reputation: 11
i have a program where i post data by radio button and send it to a php page by jquery ajax function . I want to get variable data from this page so that i can use it in my page from where i send the data . can anyone help me
<script type="text/javascript">
function getDesign() {
var radioValue = $("input[name='metal']:checked").val();
$.ajax({
type: "POST",
url: "<?php echo SITE_URL; ?>image_filter.php",
data:'metal='+radioValue,
success: function(data){
//$("#desc2").html(data);
alert(data);
}
});
}
</script>
<?php
$metal = $_POST['metal'];
$finish = $_POST['finish'];
echo $metal;
?>
i want to use the metal variable in my main page
Upvotes: 0
Views: 2375
Reputation: 746
Use dataType
option to accept the response in JSON
format.
<script type="text/javascript">
function getDesign() {
var radioValue = $("input[name='metal']:checked").val();
$.ajax({
type: "POST",
dataType: "json",
url: "<?php echo SITE_URL; ?>image_filter.php",
data: {
metal: radioValue,
finish: ''
},
success: function(data){
console.log(data.metal);
console.log(data.finish);
}
});
}
</script>
<?php
$metal = $_POST['metal'];
$finish = $_POST['finish'];
echo json_encode([
'metal' => $metal,
'finish' => $finish
]);
?>
Refer to this documentation regarding dataType
Upvotes: 1
Reputation: 1307
You can return json in PHP this way:
<?php
$metal = $_POST['metal'];
$finish = $_POST['finish'];
header('Content-Type: application/json');
$json = json_encode(array("metal"=>$metal));
echo $json;
exit;
?>
Upvotes: 0