Jump to content

Don't understand way my jquery script will not work


Go to solution Solved by Ch0cu3r,

Recommended Posts

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>

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 by Ch0cu3r

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

  • Solution

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 by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.