943,697 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 318
  • PHP RSS
Aug 18th, 2009
0

Intergrating javascript with php / mysql

Expand Post »
hello,

i have a mysql database which i use a php script to search and display results with fields make, model, price, year and would like to add extra functionality with presumably javascript to add a drop down form field as when a particular make is selected from the list then it auto displays the relevant models only for that make.

help appreciated !

pabzzmike
Last edited by pabzzmike; Aug 18th, 2009 at 3:45 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pabzzmike is offline Offline
20 posts
since May 2009
Aug 18th, 2009
0

Re: Intergrating javascript with php / mysql

Click to Expand / Collapse  Quote originally posted by pabzzmike ...
hello,

i have a mysql database which i use a php script to search and display results with fields make, model, price, year and would like to add extra functionality with presumably javascript to add a drop down form field as when a particular make is selected from the list then it auto displays the relevant models only for that make.

help appreciated !

pabzzmike
[code]
<?php

//from your 2 resultsets containing the models & makes
//create a models array for each make
$models = array();
while($makerow = mysql_fetch_assoc($makeresultset)){
$j = 0;
while($row = mysql_fetch_assoc($modelresultset)){
if($row['make_name'] == $makerow['make_name'])
$models[$j++] = "'".$row['model_name']."'";
}
}

//joiining the array items to form a string separated by commas
//overally we create an array of such strings
$newModels = array();
for($i=0; $i<count($models); $++){
$newModels[$i] = implode(", ", $models[$i]);
}

//initialising the javascript array
$modelString = "" ;
for($i=0; $i<count($newModels); $++){
$modelString .= "modelArray[$i] = $newModels[$i]; " ;
}

echo "
<script language=javascript>
function fillArea(){
var modelArray = new Array();
$modelString
var value = document.getElementById('make').value;
var dest = document.getElementById('output');
switch(value){
case '1': dest.innerHTML = 'make 1 stuff' ; break;//output the modelArry[1] items here
case 2':dest.innerHTML ='make 2 stuff'; break;//output the modelArry[2] items here
case 'n': dest.innerHTML ='make 3 stuff'; break;//output the modelArry[n] items here
}
</script>
"
?>
<form>
<select id=make onchange=fillArea()>
<option value='1'>Make 1</option>
<option value='2'>Make 2</option>
<option value='n'>Make 3</option>
</select>
<form>
<div id=output>
</div>
[code]

Not revised - though that's the idea i usually use !!
Reputation Points: 21
Solved Threads: 15
Junior Poster in Training
wilch is offline Offline
76 posts
since Aug 2007
Aug 19th, 2009
0

Re: Intergrating javascript with php / mysql

code looking fine. try it.
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP variable in link
Next Thread in PHP Forum Timeline: how to enter data in database from HTML form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC