richcleverley
MIS
Hi,
Hope this is the right forum for this one.
I have a form that has a couple of dynamic selects that pull in the relevant data for themselves form a mysql database (mix of javascript and php). These work no problems.
The problem I have, is that when ever the selects are changed and teh page refreshes to pull in the correct data for the next select then any other form data that has been input is lost.
Basically, the dynamic boxes are Country which when changed affects the contents of Region, which affects the contents of Town/city.
The user also has to input other info like title, description etc. (this is for a property rental website).
Is there an easy way to get (presumably) AJAX to control the popups so the page doesn't have to reload and lose the currently input data.
I am pretty hopeless at Javascript and haven't gone into AJAX at all, although I know it would probably be a good idea to get my head around it.
This is the code that is currently in place so you can see what I've done.
Javascript
PHP for getting the relavant data from the database
Form code
Thanks,
Richard
Hope this is the right forum for this one.
I have a form that has a couple of dynamic selects that pull in the relevant data for themselves form a mysql database (mix of javascript and php). These work no problems.
The problem I have, is that when ever the selects are changed and teh page refreshes to pull in the correct data for the next select then any other form data that has been input is lost.
Basically, the dynamic boxes are Country which when changed affects the contents of Region, which affects the contents of Town/city.
The user also has to input other info like title, description etc. (this is for a property rental website).
Is there an easy way to get (presumably) AJAX to control the popups so the page doesn't have to reload and lose the currently input data.
I am pretty hopeless at Javascript and haven't gone into AJAX at all, although I know it would probably be a good idea to get my head around it.
This is the code that is currently in place so you can see what I've done.
Javascript
Code:
function reload(form){
var val=form.country.options[form.country.options.selectedIndex].value;
self.location='index.php?country=' + val ;
}
function reloaded(form){
var val=form.country1.options[form.country1.options.selectedIndex].value;
self.location='index.php?country1=' + val ;
}
function reload1(form){
var val=form.country.options[form.country.options.selectedIndex].value;
self.location='addvilla.php?country=' + val ;
}
function reloaded1(form){
var val=form.region.options[form.region.options.selectedIndex].value;
var val1=form.country.options[form.country.options.selectedIndex].value;
self.location='addvilla.php?region=' + val + '&country=' + val1 ;
}
PHP for getting the relavant data from the database
Code:
///////// Getting the data from Mysql table for country list box//////////
$quer2=mysql_query("SELECT DISTINCT country_name,cid FROM country order by cid");
///////////// End of query for country list box////////////
/////// for region drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($country) and strlen($country) > 0){
$quer=mysql_query("SELECT DISTINCT region_name,rid FROM region where cid=$country order by region_name");
}else{$quer=mysql_query("SELECT DISTINCT region_name,rid FROM region order by region_name"); }
////////// end of query for region subcategory drop down list box ///////////////////////////
/////// for Town drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($region) and strlen($region) > 0){
$quer3=mysql_query("SELECT DISTINCT town_name,tid FROM town where rid=$region order by town_name");
}else{$quer3=mysql_query("SELECT DISTINCT town_name FROM town order by town_name"); }
////////// end of query for Town subcategory drop down list box ///////////////////////////
Form code
Code:
<form action="addvilla.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="613" border="0">
<tr>
<td height="-4" valign="top"><p>Villa Title</p></td>
<td valign="top"><p>
<input name="title" type="text" id="title" />
</p></td>
</tr>
<tr>
<td height="-2" valign="top">Country</td>
<td valign="top"><?
echo "<select name='country' onchange=\"reload1(this.form)\" class=\"formelements\"><option value='' >Country?</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['cid']==@$country){echo "<option selected value='$noticia2[cid]'>$noticia2[country_name]</option>"."<BR>";}
else{echo "<option value='$noticia2[cid]'>$noticia2[country_name]</option>";}
}
echo "</select>";
?></td>
</tr>
<tr>
<td height="-2" valign="top">Or add a new Country </td>
<td valign="top"><input name="newcountry" type="text" id="newcountry" /></td>
</tr>
<tr>
<td height="-2" valign="top">Region</td>
<td valign="top"><? echo "<select name='region' onchange=\"reloaded1(this.form)\" class=\"formelements\"><option value='' >Region?</option>";
while($noticia = mysql_fetch_array($quer)) {
if($noticia['rid']==@$region){echo "<option selected value='$noticia[rid]'>$noticia[region_name]</option>"."<BR>";}
else{echo "<option value='$noticia[rid]'>$noticia[region_name]</option>";}
}
echo "</select>";
?> </td>
</tr>
<tr>
<td height="-2" valign="top">Or add a new Region </td>
<td valign="top"><input name="newregion" type="text" id="newregion" /></td>
</tr>
<tr>
<td height="-2" valign="top">Town</td>
<td valign="top"><label>
<?
echo "<select name='town' class=\"formelements\"><option value=''>Town?</option>";
while($noticia3 = mysql_fetch_array($quer3)) {
echo "<option value='$noticia3[tid]'>$noticia3[town_name]</option>";
}
echo "</select>";
?>
</label></td>
</tr>
<tr>
<td height="-2" valign="top">Or Add a new Town </td>
<td valign="top"><input name="newtown" type="text" id="newtown" /></td>
</tr>
<tr>
<td height="-2" valign="top">ShortDescription: </td>
<td valign="top"><input name="shortdesc" type="text" id="shortdesc" /></td>
</tr>
<tr>
<td height="-2" valign="top">Departure date and time</td>
<td valign="top"><input name="dep" type="text" id="dep" /></td>
</tr>
<tr>
<td height="-2" valign="top">Arrival Date and Time</td>
<td valign="top"><input name="arr" type="text" id="arr" /></td>
</tr>
<tr>
<td height="34" valign="top"><input name="addprop" type="hidden" id="addprop" value="1" /></td>
<td valign="top"><input type="submit" name="Submit" value="Continue" />
</td>
</tr>
</table>
<p> </p>
<p> </p>
</form>
Thanks,
Richard