robert_gsfame Posted March 26, 2010 Share Posted March 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/ Share on other sites More sharing options...
o3d Posted March 26, 2010 Share Posted March 26, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/#findComment-1031985 Share on other sites More sharing options...
robert_gsfame Posted March 26, 2010 Author Share Posted March 26, 2010 i dont really get...could u please be specific :-\ Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/#findComment-1031987 Share on other sites More sharing options...
robert_gsfame Posted March 26, 2010 Author Share Posted March 26, 2010 that is only a mistype anyway Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/#findComment-1031988 Share on other sites More sharing options...
o3d Posted March 26, 2010 Share Posted March 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/#findComment-1031991 Share on other sites More sharing options...
robert_gsfame Posted March 26, 2010 Author Share Posted March 26, 2010 okay i've change it, but the problem is not yet solved..... :'( Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/#findComment-1031992 Share on other sites More sharing options...
o3d Posted March 26, 2010 Share Posted March 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/#findComment-1032002 Share on other sites More sharing options...
robert_gsfame Posted March 26, 2010 Author Share Posted March 26, 2010 instead of having the another form, i use body onload=function() ....it works but i think i have to reload the page once form submitted to have the function work Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/#findComment-1032023 Share on other sites More sharing options...
o3d Posted March 26, 2010 Share Posted March 26, 2010 instead of having the another form, i use body onload=function() ....it works but i think i have to reload the page once form submitted to have the function work Yes most definitely. Quote Link to comment https://forums.phpfreaks.com/topic/196554-dynamic-combobox-and-mysql/#findComment-1032025 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.