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

4
  • does the alert(data) show the value? what's the output of echo $metal; ? Commented Jul 16, 2019 at 11:14
  • Possible duplicate of jQuery send string as POST parameters Commented Jul 16, 2019 at 11:19
  • The explanation of the duplicate's answer: data should be an object with key/value pairs: data: { metal: radioValue }, Commented Jul 16, 2019 at 11:21
  • Check about session Commented Jul 16, 2019 at 11:26

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks vinay for your response. But how could i decode the datatype json and assign to a variable
@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?
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 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.
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
|
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;
?>

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.