2 Replies - 6883 Views - Last Post: 03 March 2014 - 08:44 AM Rate Topic: -----

#1 melati   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-March 14

Retrieve data from database into dropdownlist using ajax

Posted 01 March 2014 - 03:21 AM

hye, can you pls help me with this code. I have to retrieve all data from database using this code into my dropdownlist, but, error show that "Undefined index: q"

here is my code.


<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select the Categories:</option>
<option value="1">Order</option>
<option value="2">Management</option>

</select>
</form>
<br>
<div id="txtHint"><b>This info will be listed here.</b></div>

</body>
</html>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','','my_db');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM req WHERE id = '".$q."'";

$result = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Requirements</th>
<th>Description</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Requirements'] . "</td>";
  echo "<td>" . $row['Description'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>



thank in advance guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve data from database into dropdownlist using ajax

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: Retrieve data from database into dropdownlist using ajax

Posted 01 March 2014 - 07:17 AM

show the code for getuser.php.
Was This Post Helpful? 0
  • +
  • -

#3 cokacola   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 23-July 10

Re: Retrieve data from database into dropdownlist using ajax

Posted 03 March 2014 - 08:44 AM

I'll assume the page you listed is getuser.php
First off, it's displayed because you're asking for $_GET['q] before you even submit the request.
Since you have the PHP at the bottom of the page, it'll run regardless of whether or not you actually submit your ajax request.
To fix this, you can wrap your PHP code in an if statement and check if the variable exists.
On top of this, if what I've said is true and it's the same page, the data returned by the ajax request will contain all of your HTML as well.
So, if they are both in the same page, you can try either placing the HTML and the PHP in two separate files, or try something like this:
<?php
	if(isset($_GET['q']))
	{
		$q = intval($_GET['q']);

		$con = mysqli_connect('localhost','root','','my_db');
		if (!$con)
		  {
		  die('Could not connect: ' . mysqli_error($con));
		  }

		mysqli_select_db($con,"ajax_demo");
		$sql="SELECT * FROM req WHERE id = '".$q."'";

		$result = mysqli_query($con,$sql);

		echo "<table border='1'>
		<tr>
		<th>Requirements</th>
		<th>Description</th>
		</tr>";

		while($row = mysqli_fetch_array($result))
		  {
		  echo "<tr>";
		  echo "<td>" . $row['Requirements'] . "</td>";
		  echo "<td>" . $row['Description'] . "</td>";
		  echo "</tr>";
		  }
		echo "</table>";

		mysqli_close($con);
	}
	else
	{
?>
<html>
	<head>
		<script>
		function showUser(str)
		{
			if (str=="")
			{
				document.getElementById("txtHint").innerHTML="";
				return;
			} 
			if (window.XMLHttpRequest)
			{
				xmlhttp=new XMLHttpRequest();
			}
			else
			{
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
				}
			}
			xmlhttp.open("GET", "getuser.php?q="+str, true);
			xmlhttp.send();
		}
		</script>
	</head>
	<body>

		<form>
			<select name="users" onchange="showUser(this.value)">
				<option value="">Select the Categories:</option>
				<option value="1">Order</option>
				<option value="2">Management</option>
			</select>
		</form>
		<br>
		<div id="txtHint"><b>This info will be listed here.</b></div>

	</body>
</html>
<? } ?>



Now, if the code is actually in two separate files, I'm not sure as that should work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1