/    Sign up×
Community /Pin to ProfileBookmark

values of drop down menu

Hi all,
I am using an ajax (with PHP) based drop down menu in which each of the list values of drop down menu depends on the selection of the previous menu.
Something like: Country -> City – >District.
My problem is in validation. If i made my selection from country up to District and submitted the form, and incase i encountered with an error in inserting to the DB, then i click back button, the values of the selected city & district will be gone (reset). I tried even to store the values in a session variable, it will still show the selected values in the list, but the JS validation will still ask me to select city & District. But, country field is OK because it is not dependant on other field. My Question, is how to keep the values of the selected fields (city & district) set, so that the user will not have to make the selection again starting from country – district.

I hope you got i what mean.
Thanks in advance.
M

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Y_LessMay 25.2009 — Only HTML state is stored in most browsers for 'back', JavaScript state isn't. However how are you setting the new values? In my tests writing the HTML for the options direct seems to work fine when you click back:

document.getElementById('select_id').innerHTML = '<option>1</option><option>2</option><option>3</option>';
Copy linkTweet thisAlerts:
@themoonauthorMay 26.2009 — Hi Y_Less

Thanks for your reply, i am pasting some of my codes below:

This is part of my js file.

[code=html]function ListCities(region_id) {

var strURL="findTheCity.php?i_region_id="+region_id;
var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('theCitydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
[/code]


in my php file (findTheCity.php), i have this:
[code=php]ob_start();
session_start();
header('Content-Type: text/html; charset=windows-1256');
include("../admin/db.php");

if (isset($_GET['i_region_id'])) {
$region_id=intval($_GET['i_region_id']);
} else {
$region_id=intval($_SESSION['region_id']);
}

if (isset($_GET['city'])) {
$cityId=intval($_GET['city']);
} else {
$cityId=intval($_SESSION['city_id']);
}

$query="SELECT *
FROM city
WHERE cty_region_id='$region_id'
";
$result=mssql_query($query);
?>
<select name="city_id" onchange="getCenter(<?=$cityId?>,this.value)">
<option>Choose City</option>
<?php
while($row=mssql_fetch_array($result)) { ?>
<option value="<?=$row['cty_id']?>"><?=$row['cty_name']?></option>
<? } ?>
</select>[/code]


and in my html form, i have this:

[code=php]<tr class='trAlternate'>
<td valign='top'><br/><font color='red'>*</font>Region<br/>
";?><select name='region_id' onChange="ListCities(this.value)">

<?php
list_regions();
echo "
</select>
</td>
<td valign='top'><br/>
<font color='red'>*</font>City<br/>
<div id='theCitydiv'>
<select name='city_id'>";
echo "<option>Choose Region First</option>
</select>
</div>
</td>
<td><br/>
<font color='red'>*</font>Center<br/>
<div id='Centerdiv'>
<select name='work_in_center'>";
echo "<option>Choose City First</option>
</select>
</div>
</td>
</tr>[/code]


so, i am working with "Region->Cities->Centers" instead of "Country -> City - >District"

i am not sure where to put "innerHtml" in my code as you mentioned.

Thanks

M
Copy linkTweet thisAlerts:
@themoonauthorMay 28.2009 — Hi,

anyone ?

i was just wondering if iam supposed to paste my question in PHP thread.

:rolleyes:
×

Success!

Help @themoon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...