3 Replies - 12262 Views - Last Post: 02 March 2011 - 11:19 AM Rate Topic: -----

#1 deathmeat  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 25-August 10

combo box php

Posted 02 March 2011 - 10:52 AM

hello everyone:)i want to know how can i have a connection between javascript and php.basically what i want is to have a combo box lets say it car Make.which takes its value from database
once the user select's a car make from the combo box another combo box becomes visible and displays it .so if I select Ford from the first combo box the second combo box appears and displays Mustang etc..
this is the code of getting value from database and assigning it to combo box
<? include("connection.php") ?>
<?
$query_dispMake="SELECT make FROM car ORDER BY make";
$result_dispMake=mysql_query($query_dispMake);
 
   
echo "<select name=\"select\">\n"; 

while($query_data = mysql_fetch_array($result_dispMake)){


  echo"<option>{$query_data['make']}</option>";  

 }
 $query_dispMake2="SELECT DISTINCT model FROM car ORDER BY model";
$result_dispMake2=mysql_query($query_dispMake2);
  echo "<select name=\"second\">\n"; 
 while($query_data = mysql_fetch_array($result_dispMake2)){

  echo"<option>{$query_data['model']}</option>";   

 }
?>


Is This A Good Question/Topic? 0
  • +

Replies To: combo box php

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: combo box php

Posted 02 March 2011 - 10:55 AM

What you're looking for is a little something called AJAX.
Was This Post Helpful? 2
  • +
  • -

#3 deathmeat  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 25-August 10

Re: combo box php

Posted 02 March 2011 - 11:04 AM

View PostValek, on 02 March 2011 - 10:55 AM, said:

What you're looking for is a little something called AJAX.

thanks alot :-) what if i want to have it without the second box appearing .i mean two box being there .if you select from the first combo box then the second one loads the values related to the first.
i know how to do it in javascript without using database but when it comes to database javascript fails and i want everything to be retrieved from database
here is the code for javascript
NOTE THE FOLLOWING CODE HAS BEEN TAKEN FROM LYNDA Javascript TRAINING im learning javascript php at the moment
<?php
<html>[attachment=22694:script.zip]
<head>
	<title>Dynamic Menus</title>
	<script type="text/javascript" src="script.js">
	</script>	
</head>
<body>
<form action="dat.php" method="post">
	<select id="months">
	     
		<option value="">Month</option>
		<option value="0">January</option>
		<option value="1">February</option>
		<option value="2">March</option>
		<option value="3">April</option>
		<option value="4">May</option>
		<option value="5">June</option>
		<option value="6">July</option>
		<option value="7">August</option>
		<option value="8">September</option>
		<option value="9">October</option>
		<option value="10">November</option>
		<option value="11">December</option>
	</select>
	&nbsp;
	
	<select id="days">
		<option>Day</option>
	</select>
</form>

</body>
</html>
?>


window.onload = initForm;

function initForm() {
	document.getElementById("months").selectedIndex = 0;
	document.getElementById("months").onchange = populateDays;
}

function populateDays() {
	var monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var monthStr = this.options[this.selectedIndex].value;
	
	if (monthStr != "") {
		var theMonth = parseInt(monthStr);
					
		document.getElementById("days").options.length = 0;
		for(var i=0; i<monthDays[theMonth]; i++) {
			document.getElementById("days").options[i] = new Option(i+1);
		}
	}
}

Attached File(s)

  • Attached File  script.zip (793bytes)
    Number of downloads: 464

This post has been edited by deathmeat: 02 March 2011 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#4 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: combo box php

Posted 02 March 2011 - 11:19 AM

Use JS to Replace the Drop list or use JS DOM To Remove and Append New Items returned by AJAX
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1