backspc Posted October 9, 2012 Share Posted October 9, 2012 I am having some real trouble getting my head around php (just starting out) I have successfully implemented this triple drop down ajax script http://roshanbh.com....ax-and-php.html my problem is getting it to populate text boxes as well. <? $country=intval($_GET['country']); $link = mysql_connect('localhost', 'root', ''); //changet the configuration in required if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('db_ajax'); $query="SELECT id,statename,phone,contact FROM state WHERE countryid='$country'"; $result=mysql_query($query); ?> <select name="state" onchange="getCity(<?=$country?>,this.value)"> <option>Select State</option> <? while($row=mysql_fetch_array($result)) { ?> <option value=<?=$row['id']?> <?=$row['statename']?> <?=$row['phone']?> <?=$row['contact']?></option> <? } ?> </select> <br/> on change update this to the selected contact <br/> <input type=text value=<?=$row['contact']?>> <br/> on change update this to the selected phone <br/> <input type=text value=<?=$row['phone']?>> I have been battling this for about a week now it getting beyond a joke Free hugs for help Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 You need to post your Javascript as well. Quote Link to comment Share on other sites More sharing options...
backspc Posted October 10, 2012 Author Share Posted October 10, 2012 Hi Jessica, Thanks for the reply. That java I am using for this part is: <script language="javascript" type="text/javascript"> // Roshan's Ajax dropdown code with php // This notice must stay intact for legal use // Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com // If you have any problem contact me at http://roshanbh.com.np function getXMLHTTP() { //fuction to return the xml http object var xmlhttp=false; try{ xmlhttp=new XMLHttpRequest(); } catch(e) { try{ xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e1){ xmlhttp=false; } } } return xmlhttp; } function getState(countryId) { var strURL="findState.php?country="+countryId; var req = getXMLHTTP(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById('statediv').innerHTML=req.responseText; } else { alert("1 There was a problem while using XMLHTTP:\n" + req.statusText); } } } req.open("GET", strURL, true); req.send(null); } } function getCity(countryId,stateId) { var strURL="findCity.php?country="+countryId+"&state="+stateId; var req = getXMLHTTP(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById('citydiv').innerHTML=req.responseText; } else { alert("2 There was a problem while using XMLHTTP:\n" + req.statusText); } } } req.open("GET", strURL, true); req.send(null); } } </script> After the first drop down is selected the second one is populated with the code from above ie <select name="state" onchange="getCity(<?=$country?>,this.value)"> <option>Select State</option> <? while($row=mysql_fetch_array($result)) { ?> <option value=<?=$row['id']?> <?=$row['statename']?> <?=$row['phone']?> <?=$row['contact']?></option> that is when I would like a text box also populated with just the phone and another with just the contact. Hope this makes sense Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 As one of our member's signatures says, "Java is to Javascript as fun is to funeral." You have javascript, it is not java. You may want to look into using jQuery. The problem you're having is that inputs have a value, whereas textareas have innerHTML. Try changing the innerHTML to value. If that doesn't work, definitely look at jQuery. It's so much easier. Quote Link to comment Share on other sites More sharing options...
backspc Posted October 14, 2012 Author Share Posted October 14, 2012 (edited) Thanks I didn't understand about java & javescript so did a bit of looking up (thanks for pointing it out for me) I tried the innerHTML values but that didn't get me anywhere in the end I used <?phpmysql_select_db('db_ajax'); $query="SELECT id,statename,phone,contact FROM state WHERE countryid='$country'"; $result2=mysql_query($query); ?> <?php while ($row = mysql_fetch_assoc($result2)) { $text3 =$row['contact']; } ?> <select style='display:none' id="Text3"> <option value="val0"><?php echo htmlentities($text3); ?></option> </select> <script language="Javascript" type="text/javascript"> <!-- function showSelected() { var selObj = document.getElementById('Text3'); var txtText3Obj = document.getElementById('txtText3'); var selIndex = selObj.selectedIndex; txtText3Obj.value = selObj.options[selIndex].text; //--> </script> <input type="button" value="Insert" onclick="showSelected();" /> <input type="text" id="txtText3" name="contact"> There is a better way but this works for me at the moment so on to the next issue... Thanks for your help Edited October 14, 2012 by backspc 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.