0

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

Vinay Patil
  • 736
  • 6
  • 19

2 Answers2

1

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

Vinay Patil
  • 736
  • 6
  • 19
  • Thanks vinay for your response. But how could i decode the datatype json and assign to a variable – Panda Surya Jul 16 '19 at 11:23
  • @PandaSurya please check the awnser again I have modified the success function. You will get the json data in object format which you can access as shown in the above answer. Also can you specify the reason for give -ve vote? – Vinay Patil Jul 16 '19 at 11:36
  • Using JSON will not solve the problem, `data` in the `success` function can perfectly be raw text, it will work all right. See the suggested duplicate. – Kaddath Jul 16 '19 at 11:37
  • @Kaddath accessing data in raw test or using object will result in same thing. Here is the modified statement for success `$("#desc2").html(data.metal);` which also result the same. – Vinay Patil Jul 16 '19 at 11:41
  • It will work since your last edit because you changed the way the value is passed in the `$.ajax` options accordingly to the duplicate suggestion, not because you use JSON (**and this should be the explanation in the answer or else it will mislead the OP**). And no, using raw text or JSON will not result in the same thing, if you use raw text, `data.metal` will be undefined.. note I didn't downvote your answer yet – Kaddath Jul 16 '19 at 11:48
  • @Kaddath yes if using the raw test the `data.metal` will be undefined, but for json it will work correct? – Vinay Patil Jul 16 '19 at 12:22
0

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;
?>
Shivani Sonagara
  • 1,299
  • 9
  • 21
  • Using JSON will not solve the problem, `data` in the `success` function can perfectly be raw text, it will work all right. See the suggested duplicate. – Kaddath Jul 16 '19 at 11:37