Jump to content

dynamic combobox and mysql


robert_gsfame

Recommended Posts

I have dynamic combobox

 

below is the html code and javascript

function show(){

if (document.form1.country.value == "Brazil"){document.getElementById('city').innerHTML = "<select name='mycity' id='mycity'><option value='ABC' id='ABC'>ABC</option></select>"}

 

<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">

<select name="country"><option value="brazil" id="brazil" onchange= onchange="show()">Brazil</option>

 

<div id="city"></div>

<input type="submit" value="submit>

</form>

 

now the problem is when i retrieve the data from mysql database, let say i have ABC already stored in the table (column 'city') then i wish to have it straight away once login...how can i do that as i have used onchange event and that will hide the 2nd combobox if 1st combobox not yet chosen

Link to comment
https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/
Share on other sites

Just so you know this will create problems if you use the same property in a tag more than once. It uses the first instance and ignore the rest. Remove the first "onchange=" occurance.

<select name="country"><option value="brazil" id="brazil" onchange= onchange="show()">Brazil</option>

 

change

<select name="country"><option value="brazil" id="brazil" onchange= onchange="show()">Brazil</option>

to

<select name="country"><option value="brazil" id="brazil" onchange="show()">Brazil</option>

 

btw my suggestion won't fix your problem, i'm just pointing out a problem.

Once the user is logged in, I assume you populate a global variable or a session variable. Say e.g. $_SESSION['city_id']. Then you could do this:

 

<?PHP if ($loggedin) { ?>
<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<select name="country"><option value="brazil" id="brazil">Brazil</option></select>
<select name="city"><?PHP
//loop through table of cities and check if city id == $_SESSION['city_id']
while () {
	echo '<option value="brazil" id="brazil" '.(city id == $_SESSION['city_id']?'selected':'').'>Brazil</option>';
} ?></select>
<input type="submit" value="submit>
</form>
<?PHP } else { ?>
<script>
function show(){
	if (document.form1.country.value == "Brazil") {
		document.getElementById('city').innerHTML = "<select name='mycity' id='mycity'><option value='ABC' id='ABC'>ABC</option></select>";
	}
}
</script>
<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<select name="country"><option value="brazil" id="brazil" onchange="show()">Brazil</option>
<div id="city"></div>
<input type="submit" value="submit>
</form>
<?PHP } ?>

 

Please know that this is just a concept and has not been tested and is psuedo-ish. Hope this put you on the right track.

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.