1

Here is my JavaScript:

                    $.ajax({
                        url: 'CheckColorPrice.php',
                        type: 'POST',
                        data: {
                            url: '<?php echo $LINK;?>',
                            ColorId: ColorNumber
                        },
                        dataType: 'json',
                        success: function (data) {
                            $('#LoadingImage').hide();
                                $("#PRICE").text("£ " + data["price"]);                             
                        }
                    });

Here is CheckColorPrice.php:

<?PHP
$url = $_POST['url'];
$ColorId = $_POST['ColorId'];       
if(isset($_POST['url']))
{   
    libxml_use_internal_errors(true); 
    $doc = new DOMDocument();
    $doc->loadHTMLFile($url);

    $xpath = new DOMXpath($doc);

    $DataVariants = $xpath->query('//span[@class="ImgButWrap"]/@data-variants')->item(0)->nodeValue;

    $jsonStart = strpos($DataVariants, '[');
    $jsonEnd = strrpos($DataVariants, ']');

    $collections = json_decode(substr($DataVariants, $jsonStart, $jsonEnd - $jsonStart + 1));   

    foreach ($collections as $item) {
        $ColVarId = $item->ColVarId;

        $SizeNames = [];
        $SellPrice = [];
        foreach ($item->SizeVariants as $size) {
            $SizeNames[] = $size->SizeName;
            $SellPrice[0] = $size->ProdSizePrices->SellPrice;
        }
        $names = implode(',', $SizeNames);
        $price = implode('', $SellPrice);

    if($ColVarId == $ColorId){
                $healthy2 = array('£',' ','Â');
                $yummy2   = array('','','');
                $price = str_replace($healthy2, $yummy2, $price);
                $PRICE = $price;
            echo "price: ",  json_encode($PRICE), "\n";

    }
}

}   

?>

The result from CheckColorPrice.php is looking just like this:

price: "37.99"

Where is my mistake, why it is not receiving the response properly. I don't get it at all... Can you help me out ?

Thanks in advance!

1
  • You have the following line: echo "price: ", json_encode($PRICE), "\n"; ~ this means you are returning a string rather than a json object. If you are intending to work with json data the php should do something like:- echo json_encode( array( 'price'=>$price ) ); Commented Aug 21, 2015 at 15:44

3 Answers 3

4

You're not returning json. You're returning plain text which contains some json:

        echo "price: ",  json_encode($PRICE), "\n";
             ^^^^^^^^^^

That'd look like

    price: "$9.99"

which is NOT valid json.

You need to return an array for your JS code to work:

echo json_encode(array('price' => $PRICE));

which would output:

{"price":"$9.99"}
Sign up to request clarification or add additional context in comments.

Comments

2

Add the following header to your script:

header('Content-type: application/json'); header("Content-Disposition: inline; filename=ajax.json");

Also change the line

echo "price: ",  json_encode($PRICE), "\n";

to

echo json_encode(array('price'=>$PRICE));

Hope that helps

Comments

0

First thing, in your ajax request, add this parameter :

dataType : 'json'

Then, your response is not a correct json. You can return this :

echo json_encode(array("price"=>$PRICE));

Comments

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.