0

I have the following controller:

public function getPrice()
{
    $id = $this->input->post('q');
    $data['price'] = $this->emodel->get_peUniformPrice($id);

    echo json_encode($data);
}

which outputs this:

"{\"price\":[{\"Price\":\"250\"}]}"

How can I make it like 250? My jQuery:

function showPrice(size) {
    $.ajax({
        type: "POST",
        url: "<?php echo site_url('enrollment/getPrice/');?>",
        data: {
            q: size
        },
        success: function(data) {
            $("#txtpeUniform").val(data);
        },
    });
}
1
  • You can use data.price[0].Price to get it. Commented Mar 1, 2016 at 10:46

2 Answers 2

2

I can see that you are using jQuery.. if you want to turn the json object into a javascript object you could do something like

var convertedObject = $.parseJSON($data);
alert(convertedObject.Price);

what this effectively does is converts your Json string into a javascript object which you can reference the properties out of and the get the value from these properties.. let me give you another example

var jsonString = {'Firstname':'Thiren','Lastname':'Govender'};
var jObject = $.parseJSON(jsonString);
console.log(jObject.Firstname) // this will output Thiren.
console.log(jObject.Lastname) // this will output Govender.

modify your code

function showPrice(size) {
    $.ajax({
        type: "POST",
        url: "<?php echo site_url('enrollment/getPrice/');?>",
        data: {
            q: size
        },
        success: function(data) {
            console.log(data); // make sure this is returning something..
            $("#txtpeUniform").val(data);
        },
    });
}

I hope this helps you..

Regards

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

8 Comments

type out what gets displayed in the log from the above modification
I posted the output in console log
Okay so its an array of prices, could you try this and give me the output. 'var priceData = $.parseJSON(data); console.log(priceData);'
it displays in the console log this {"price":[{"Price":"500"}]}
try this "$("#txtpeUniform").val(data.price[0].Price);"
|
2

The $.ajax method will automatically deserialise the string to an object for you, so you simply need to access the required property. Assuming that you only ever want to retrieve the first price returned in the price array, you can access it directly by index. Try this:

success: function(data) {
    $("#txtpeUniform").val(data.price[0].Price); // = 250
},

7 Comments

Have you checked the network console to ensure the AJAX request is successful and that data is being returned?
this is what the console log prints Uncaught TypeError: Cannot read property '0' of undefined.....
That means that the price property is empty, or doesn't exist. What do you see in the console if you add console.log(data) to the above?
this is {"price":[{"Price":"250"}]}
I am at a loss then, as that should be working fine: jsfiddle.net/fceje6kd
|

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.