vincent marc villafuerte vincent marc villafuerte - 3 months ago 6
Ajax Question

How to select the result from data success of ajax?

These are my code:

$("#promo_form").submit(function(stay){
$.ajax ({
type: "post",
url: "<?=base_url()?>promo/code_validate",
data: $("#promo_form").serialize(),
success: function(data){
$("#myModalLabel").html(data);
}
});
stay.preventDefault();
});


And the success data result below:


$result1 = "code is invalid";

$result2 = "promo unavailable";


How can I select these and retrieve it?

This is what I did below but now working.

$("#myModalLabel").html(data->result1);

Answer

Do this

Function controller

function code_validate()
{
    echo json_encode(array('result1'=>'Code is invalid',
                           'result2'=>'Promo not available');
}

Javascript

$("#promo_form").submit(function(stay){
    $.ajax ({
        type: "post",
        url: "<?=base_url()?>promo/code_validate",
        data: $("#promo_form").serialize(),
        success: function(data){
            var mydata = JSON.parse(data);
            console.log(mydata.result1);
            console.log(mydata.result1);
        }
    });
    stay.preventDefault();
});

You must return it as JSON.

Goodluck meyt