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
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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.