Piba Posted April 3, 2008 Share Posted April 3, 2008 Hi guys, I have a drop down menu, the user must choose form this dropdown and when he choose, another dropdown menu will show up according to what he choose So, the second drop down is called in other page... My problem is the value in the second dropdown menu won't be saved if the page is refreshed!! I know how to save the value of the first one: <select name=dropdown1> <option name=option1 value=1 <? if ($_POST['dropdown1']=="1") echo selected=selected; ?> >option1</option> </select> But how could i save the value of the second dropdown menu ??? Hope i'm explaining my problem well thanks alot, Piba Quote Link to comment Share on other sites More sharing options...
mort Posted April 4, 2008 Share Posted April 4, 2008 maybe add an onchange submit to an AJAX/PHP script which would add sessions to state which second dropdown should be, and what should be selected? then when it reloads it would figure out what option to select from the session Quote Link to comment Share on other sites More sharing options...
Piba Posted April 7, 2008 Author Share Posted April 7, 2008 Hello mort, First of all, sorry for late but i got busy.... Could you explzin more how i write in onchange?? Thanks alot Quote Link to comment Share on other sites More sharing options...
MYTH Posted April 8, 2008 Share Posted April 8, 2008 hi you shall use like this <select name='dropdown2' onchange='save(this.value);'></select> you should use ajax program you should pass 'selected value' to the insert query page. for eg:- function save(str) { var url = 'query.php?dropdown2='+str; ..... ..... } you should pass like this to avoid page refresh.. Quote Link to comment Share on other sites More sharing options...
Piba Posted April 9, 2008 Author Share Posted April 9, 2008 Hi Myth Thnaks for your replay.. i'm afraid that i dont understand exactly how to make this ??? Becasue i have already onchange in the select Here is the code of select1: <html> <head> <script src="selectuser.js"></script> </head> <body><form> Select a User: <select name="dropdown1" onchange="showUser(this.value)"> <option value="1" <? if(isset($_POST['dropdown1'])) if($_POST['dropdown1']==1) echo $_POST['dropdown1']; ?>>Peter Griffin</option> <option value="2" <? if(isset($_POST['dropdown1'])) if($_POST['dropdown1']==2) echo $_POST['dropdown1']; ?>>Lois Griffin</option> <option value="3" <? if(isset($_POST['dropdown1'])) if($_POST['dropdown1']==3) echo $_POST['dropdown1']; ?>>Glenn Quagmire</option> <option value="4" <? if(isset($_POST['dropdown1'])) if($_POST['dropdown1']==4) echo $_POST['dropdown1']; ?>>Joseph Swanson</option> </select> </form><p> <div id="txtHint"><b>User info will be listed here.</b></div> </p></body> </html> Then the js page: var xmlHttpfunction showUser(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="getuser.php" url=url+"?q="+str url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) }function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } }function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } and finally the select2: <?php $q=$_GET["q"]; $con = mysql_connect('localhost', 'peter', 'abc123'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ajax_demo", $con); $sql="SELECT * FROM user WHERE id = '".$q."'"; $result = mysql_query($sql); <select name=dropdown2> while($row = mysql_fetch_array($result)) { echo "<option name=info value='$row['FirstName']'>"; } echo "</table>"; mysql_close($con); ?> So, how could i save the second value in dropdown2 when the page relode?? I don't think onchange in dropdown2 will work!???!??! Could help plz Quote Link to comment Share on other sites More sharing options...
XoSilenceoX Posted April 16, 2008 Share Posted April 16, 2008 <select name="dropdown1" onchange="showUser(this.value)"> simply add another behind it. Theres no limitations on the amoutn of functions that can be executed. <select name="dropdown1" onchange="showUser(this.value); function2(); function3(); etc.."> Would execute all those commands onchange. Quote Link to comment Share on other sites More sharing options...
shankar23 Posted April 17, 2008 Share Posted April 17, 2008 You just read the ajax in w3schools clearly.. there it is given... thanks shang.. Quote Link to comment 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.