rdkurth Posted December 16, 2013 Share Posted December 16, 2013 I am having a problem understanding why when I run my script and I put the results inside a select statement it does not show the values but when I remove the select statement the values or visible . The following code will show you what I mean. If you comment out the select statement in the html script it will work. I don't understand way <?php // CONNECT TO THE DATABASE $DB_NAME = 'notary'; $DB_HOST = 'localhost'; $DB_USER = 'root'; $DB_PASS = ''; $db=$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } ?> <?php include("db.php"); $sql='SELECT * FROM customer'; $result=$db->query($sql); while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) { echo "<option value=" . $row['name'] . ">" .$row['name'] . "</option>"; } <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>ready demo</title> <script src="js/jquery.js"></script> <script> $( document ).ready(function() { $.ajax({ //create an ajax request to load_page.php type: "GET", url: "php/display.php", dataType: "text", //expect html to be returned success: function(response){ $("#responsetext").text(response); //alert(response); } }); /*$( "p" ).text( "The DOM is now loaded and can be manipulated." );*/ }); </script> </head> <body> <div align="center"> <select name="customer"> <!--<---comment out this line--> <div id="responsetext"> </select> <!--<---comment out this line--> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/284790-dont-understand-way-my-jquery-script-will-not-work/ Share on other sites More sharing options...
Ch0cu3r Posted December 16, 2013 Share Posted December 16, 2013 (edited) Because you have invalid html code, you've opened two divs but only closed one and you have wrapped the responsetext div around the closing select tag (</select>). Only <option> tags should be within a <select> tag. To make it easier I would apply the responsetext id to the select tag. <select name="customer" id="responsetext"> </select> It works when you comment out the select tags because the browser tries to fix the invalid html code (quirks mode). Edited December 16, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284790-dont-understand-way-my-jquery-script-will-not-work/#findComment-1462443 Share on other sites More sharing options...
rdkurth Posted December 16, 2013 Author Share Posted December 16, 2013 Because you have invalid html code, you've opened two divs but only closed one and you have wrapped the responsetext div around the closing select tag (</select>). Only <option> tags should be within a <select> tag. To make it easier I would apply the responsetext id to the select tag. <select name="customer" id="responsetext"> </select> It works when you comment out the select tags because the browser tries to fix the invalid html code (quirks mode). I fixed the div errors But it still did not work </head> <body> <select name="customer"> <div id="responsetext"></div> </select> </body> </html> </head> <body> <select name="customer"id="responsetext"> </select> </body> </html> I put it in the select tag still did not work. This is frustrating I start to think I have jquey and ajax figured out and then this happens. any other ideas Quote Link to comment https://forums.phpfreaks.com/topic/284790-dont-understand-way-my-jquery-script-will-not-work/#findComment-1462478 Share on other sites More sharing options...
Solution Ch0cu3r Posted December 16, 2013 Solution Share Posted December 16, 2013 (edited) I got it to work using .html() instead of .text() $("#responsetext").html(response); Also setup your select tag as <select name="customer" id="responsetext"> </select> Edited December 16, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284790-dont-understand-way-my-jquery-script-will-not-work/#findComment-1462481 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.