javascript - Ajax within a loop -
i have troubles using ajax within loop. loop , ajax work fine on own, combining them trouble starts.
below test script wrote see if script works before make rest. idea java script gets $numbers database , performs loop amount got (so in example 7 times).
each time ajax has send $numbers php search in database %color corresponding value of $numbers, since test took "check" instead. used target specific div corresponds value of $numbers en $color (in case $number , check).
@ moment i've tested while loop , ajax separately , work perfect, combining them goes wrong. while ajax script nothing freeze browser.
i've tried loop nothing except giving me uncaught typeerror: cannot set property 'innerhtml' of undefined.
this test script.
<?php $number = 7; ?><?php if(isset($_post["number"])){ echo 'check'; exit(); } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>test</title> <link rel="icon" href="favicon.ico" type="image/x-icon"> <script src="../scripts/main.js"></script> <script> function testwhile() { var number = <?php echo $number ?>; while (number > 0) { var x = document.getelementsbyclassname(number); x[0].innerhtml = "check!"; var number = number - 1; } } function testajax() { var number = <?php echo $number ?>; var colour = "check"; var ajax = ajaxobj("post", "test1.php"); ajax.onreadystatechange = function () { if (ajaxreturn(ajax) == true) { var colour = ajax.responsetext; var x = document.getelementsbyclassname(colour + ' ' + number); x[0].innerhtml = "check!"; } } ajax.send("number=" + number); } function testwhileajax(){ var number = <?php echo $number ?>; while (number > 0) { var colour = "check"; var ajax = ajaxobj("post", "test1.php"); ajax.onreadystatechange = function () { if (ajaxreturn(ajax) == true) { var colour = ajax.responsetext; var x = document.getelementsbyclassname(colour + ' ' + number); x[0].innerhtml = "check!"; var numbers = numbers - 1; } } ajax.send("number=" + number); } } function testloopajax(){ (number = <?php echo $number ?>; number > 0; number--){ //var number = number; var colour = "check"; var ajax = ajaxobj("post", "test1.php"); ajax.onreadystatechange = function () { if (ajaxreturn(ajax) == true) { var colour = ajax.responsetext; var x = document.getelementsbyclassname(colour + ' ' + number); x[0].innerhtml = "check!"; } } ajax.send("number=" + number); } } </script> </head> <body> <div onclick="testajax()">check ajax!</div> <div onclick="testwhile()">check while!</div> <div onclick="testwhileajax()">check both!</div> <div onclick="testloopajax()">check loop!</div> <div class="1 check">check?</div> <div class="2 check">check?</div> <div class="3 check">check?</div> <div class="4 check">check?</div> <div class="5 check">check?</div> <div class="6 check">check?</div> <div class="7 check">check?</div> </body> </html>
any appreciated. :)
Comments
Post a Comment