Venelin
Venelin

Reputation: 3308

Ajax post is not returning response

Here is my code:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    var ColorId = "1";
    $( "#targetButton" ).click(function() {
            $.ajax({
                url: 'checkcolors.php',
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    var arr = data.msg.split(',');
                    arr.forEach(function(id){
                        $('#' + id.trim()).hide();
                    });
                    //$('#target').html(data.msg);
                },
                data: ColorId
            });
    });     

}); 
</script>
<button type="button" id="targetButton">Send</button>
<div class="BlackAndWhite" id="24604682">24604682</div>
<div class="BlackAndWhite" id="24604682x">24604682x</div>
<div class="BlackAndWhite" id="24604679">24604679</div>
<div class="BlackAndWhite" id="24604621">24604621</div>

Here is how the result looks like from checkcolors.php:

24604603, 24604684, 24604640, 24604609, 24604682, 24604686, 24604681, 24604689, 24604602, 24604679, 24604680, 24604622, 24604685, 24604683, 24604621, 24604677, 24604688,

And here is the code from checkcolors.php:

<?PHP
$url = 'http://www.sportsdirect.com/dunlop-mens-canvas-low-top-trainers-246046?colcode=24604622';
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 = [];
    foreach ($item->SizeVariants as $size) {
        $SizeNames[] = $size->SizeName;
    }

    if (in_array("7", $SizeNames)) {
        echo "$ColVarId, ";
    }   
}

?>

When i click the button i am watching the browser console for any warnings or errors but there are none. Somehow it is not working and i do not know why.

It is supposed to hide all div elements with the same ids given from the checkcolors.php response, but it is not working. Why ?

Can you please help me out?

Thanks in advance!

Upvotes: 0

Views: 104

Answers (2)

QuentinG
QuentinG

Reputation: 101

In your php script "checkcolors.php", you must return a JSON file.

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

    $SizeNames = [];
    foreach ($item->SizeVariants as $size) {
        $SizeNames[] = $size->SizeName;
    }

    if (in_array("7", $SizeNames)) {
        $res[] = $ColVarId;
    }   
}

echo json_encode($res);

Then in your javascript, you replace arr by data.

data.forEach(function(id){
                    $('#' + id.trim()).hide();
                });

Upvotes: 0

Neil Villareal
Neil Villareal

Reputation: 637

Try to change this line:

var arr = data.msg.split(',');

to this:

var arr = data.split(',');

Upvotes: 1

Related Questions