Jump to content

Ajax - PHP Dropdown list


klosie

Recommended Posts

Hi i need help

 

I am making a dropdown list with ajax and php, everything is ok except for the results of the second dropdown.. When i choose an option the dropdown appears withou any value in it.

 

Here i post my code, i hope someone can help me and tell me what i am doing wrong.

 

AJAX

var xmlhttp;

function showUser(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="ajaxcalling.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("proyectos").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  XMLHttpRequest.overrideMimeType('text/xml');

  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

 

PHP

<?php
$q = $_GET['catname'];
echo "<td align=\"right\"><b>Proyecto:</b></td> ";
echo "<td><select name=\"subcatname\">";
$querycat4 = "SELECT subcatname FROM proyectos WHERE catname ='".$q."'";
$resultcat4 = mysql_query($querycat4) or die ("Couldn't execute query - CATECON.PHP"); 
while($rowcat4 = mysql_fetch_array($resultcat4)) {
$subcatname = $rowcat4['subcatname'];
echo "<OPTION value=\"$subcatname\">$subcatname</OPTION>";
}
echo "</select></td></tr>";
?>

 

and the place where it suppose to appear

 

echo "<table bgcolor=\"#ECECEC\" cellpadding=\"3\" cellspacing=\"0\" align=\"center\" class=\"black\" border=\"0\">";
echo "<tr><td bgcolor=\"#ECECEC\" align=\"center\"><b>Crear tarea</b></td><td bgcolor=\"#F2F2F2\" align=\"center\"><b>Clientes</b></td></tr>";
echo "<tr><td>";
echo "<table bgcolor=\"#ECECEC\" cellpadding=\"3\" cellspacing=\"0\" align=\"center\" class=\"black\">";
echo "<tr><form name=\"form1\" method=\"post\"  onSubmit=\"return (formCheck(this))\" action=\"complete.php?getid=1\">";
echo "<td align=\"right\"><b>Cliente:</b></td> ";
echo "<td><select name=\"catname\" onchange=\"showUser(this.value)\" >";
include 'includes/catecon.php';
echo "</select></td></tr>";
echo "<div id=\"proyectos\"></div>";

Link to comment
https://forums.phpfreaks.com/topic/172596-ajax-php-dropdown-list/
Share on other sites

Hi

 

That the drop down appears but with nothing in it suggests that the AJAX stuff is working, just that there are no matching records for the catname passed through.

 

However you appear to pass a variable called q in the URL, but the php expects one called catname.

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.