JoshEir Posted January 27, 2021 Share Posted January 27, 2021 (edited) I'm trying to write an ecommerce site and was doing well until this one. It has has taken me a few days...can't get it. It won't display answerHtml in the pear div using a string. The idea is to use a variable for the pear div and to place apple div into it. The error is : fails with cannot set property 'innerhtml' of null . html> <body> <script> var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var jsonData = JSON.parse(this.responseText); var answerHtml = jsonData.htmlstuff; var pear1 = jsonData.div; //fails with cannot set property 'innerhtml' of null document.getElementById(pear1).innerHTML = answerHtml; } }; xmlhttp.open("GET", "div2.php", true); xmlhttp.send(); </script> </body> </html> <?php $host = 'localhost'; $user = 'root'; $pass = ''; $database = 'ecommerce'; $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false ); $dbo3 = new PDO("mysql:host=$host;dbname=$database", $user, $pass, $options); $var = "pear"; $string = " <div> <div id = \"$var\" >here </div> <div id = \"apple\"> <button type=\"button\" onclick=\"loadDoc()\">Change Content</button> </div> </div> "; $myObj = new stdClass(); $myObj->htmlstuff = $string; $myObj->div = $var; //Encode the data as a JSON string $jsonStr = json_encode($myObj); echo $jsonStr; ?> Thank you for your time. Edited January 27, 2021 by JoshEir fix comment Quote Link to comment https://forums.phpfreaks.com/topic/312058-ajax-variable-for-div-id-not-working/ Share on other sites More sharing options...
requinix Posted January 27, 2021 Share Posted January 27, 2021 I think you've got your order of operations mixed up. The pear DIV is inside the AJAX's HTML, so right now you're trying to insert that HTML into itself. There's also nothing in there targeting the "apple" element. 1 hour ago, JoshEir said: The idea is to use a variable for the pear div and to place apple div into it. The pear DIV has to exist on the page for you to insert something into it. It should not be in the response itself but rather already on the page, and the response would be just the apple part. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312058-ajax-variable-for-div-id-not-working/#findComment-1584047 Share on other sites More sharing options...
JoshEir Posted January 27, 2021 Author Share Posted January 27, 2021 (edited) May the DIV ID be a variable or does it need to be known in advance with a real id? Edited January 27, 2021 by JoshEir Quote Link to comment https://forums.phpfreaks.com/topic/312058-ajax-variable-for-div-id-not-working/#findComment-1584049 Share on other sites More sharing options...
requinix Posted January 27, 2021 Share Posted January 27, 2021 It can be whatever, as long as stuff exists in the way it needs to exist at the moment it needs to exist. So you can getElementById a variable, sure, but the thing you're trying to find has to be on the page - not just sitting in a variable you got from AJAX. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312058-ajax-variable-for-div-id-not-working/#findComment-1584050 Share on other sites More sharing options...
JoshEir Posted January 28, 2021 Author Share Posted January 28, 2021 Thanks for your help, I had never created my own div before, making them dynamically. I just completed the Administrator's product system. For the rest, I plan to use modern PHP instead of the Vanilla variety. Josh Quote Link to comment https://forums.phpfreaks.com/topic/312058-ajax-variable-for-div-id-not-working/#findComment-1584096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.