Jump to content

[SOLVED] Ajax dual dropdown help


paradox28491

Recommended Posts

So I've got two dropdowns. One is coded into the html document and the other is to be populated via ajax. I've got it all set up, but it isn't working at all. The first dropdown is there, you click an option from it and the second dropdown pops up with no values.

 

I can't figure out what I'm doing wrong, maybe someone here can? I believe the problem is somewhere in my databaseQuery.php.

 

databaseQuery.php

<?php
require_once("Connections/connection.php"); // database connection

// Retrieve data from Query String
$brand = $_GET['brand'];

//build query
$query = "SELECT * FROM mobo WHERE mobo_brand = '$brand'";
$result 	= @mysql_query($query); 
$rowModel 	= mysql_fetch_array($result);

/////////////////////////////////////////////////

//Build Result String
$dropdown = "<select name=\"model\">";
$dropdown .= "<option value=\"\">Select Model</option>";


// Insert a new row in the table for each person returned
do {

$dropdown .= "<option value=".$rowModel['mobo_id'].">".$rowModel['mobo_model']."</option>";

}while ($rowModel = mysql_fetch_array($result));

$dropdown .= "</select>";


echo $dropdown;
?>

 

ajaxmain.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ajax</title>
<script language="javascript" type="text/javascript" src="ajax.js"></script>
<link href="ajax.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p class="style1">AJAX dynamic dropdowns </p>

<form name='myForm'>
  
  <table width="200" border="0">
    <tr>
      <td width="114">
  
  <select name="brand" onchange="ajaxFunction()" id="brand">
        <option value="">Select Brand</option>
        <option value="ASRock">ASRock</option>
        <option value="ASUS">ASUS</option>
        <option value="BIOSTAR">BIOSTAR</option>
        <option value="DFI">DFI</option>
      </select>
  
  </td>
      <td width="76"><div id='ajaxDiv'></div></td>
    </tr>
  </table>

</form>

</body>
</html>

 

ajax.js

// JavaScript Document
function ajaxFunction(){

var ajaxRequest;  // magic variable

try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {

		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your browser broke!");
			return false;
		}
	}
}

// Receive Data Function 
ajaxRequest.onreadystatechange = function(){

	if(ajaxRequest.readyState == 4){
		var ajaxDisplay = document.getElementById('ajaxDiv');
		ajaxDisplay.innerHTML = ajaxRequest.responseText;
	}

}

var brand = document.getElementById('brand').value;

var queryString = "?brand=" + brand;
ajaxRequest.open("GET", "databaseQuery.php" + queryString, true);
ajaxRequest.send(null); 
}

Link to comment
https://forums.phpfreaks.com/topic/147662-solved-ajax-dual-dropdown-help/
Share on other sites

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.