php - unable to fetch value from second dynamic dependent drop down box -
i hope whole day trouble @ least on now. have 2 drop down box. after submitting have first's dropdown box value in action page. second drop down box dependent on value selected in first dropdown box.
this ajax code in head section
<script> function getxmlhttp() { //function return 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 getscreen(strurl) { var req = getxmlhttp(); if (req) { req.onreadystatechange = function() { if (req.readystate == 4) { // if "ok" if (req.status == 200) { document.getelementbyid('screendiv').innerhtml=req.responsetext; } else { alert("there problem while using xmlhttp:\n" + req.statustext); } } } req.open("get", strurl, true); req.send(null); } } </script> and body of page
<form method="post" action="a.php" name="form1"> <table width="60%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="150">device name</td> <td width="150"><select name="device" onchange="getscreen('finddevice.php?device='+this.value)"> <option value="">select device</option> <option value="1">iphone 3</option> <option value="2">iphone 4</option> <option value="3">ipad </option> <option value="4">android </option> </select></td> </tr> <tr style=""> <td>screen</td> <td ><div id="screendiv"><select name="screen"> <option>select screen</option> </select></div></td> </tr> </table> <input type="submit" value="submit" /> </form> it call finddevice.php page has following codes
<? $device=$_request['device']; $link = mysql_connect('localhost', 'root', ''); //changet configuration in required if (!$link) { die('could not connect: ' . mysql_error()); } mysql_select_db('future'); $query="select screen screen device_id=$device"; $result=mysql_query($query); ?> <select name="screen"> <option>select screen</option> <? while($row=mysql_fetch_array($result)) { ?> <option value><?=$row['screen']?></option> <?php $row['device_id'] ?> <? } ?> </select> its working , bringing value table second dropdown box upon selection of value first dropdownbox. when submit data, can access first dropdown box value in a.php file using $_post method. means can value of $_post['device'] there not value $_post['screen']. please me out.
in finddevice.php, line 14 (as above) seem have missed value of option in 2nd drop down
mean <option value> should <option value="1">
try filling in id or unique value options in 2nd drop down
unless fill in values of options, 2nd drop down submit blank value a.php
finddevice.php:
<?php $device=$_request['device']; $link = mysql_connect('localhost', 'root', ''); //changet configuration in required if (!$link) { die('could not connect: ' . mysql_error()); } mysql_select_db('future'); $query="select id, screen screen device_id=$device"; //if screen table contains id field ... //replace id primary key or unique identifier screen table //if there aint primary key in "screen" table, use field unique //or create primary key $result=mysql_query($query); ?> <select name="screen"> <option>select screen</option> <?php while($row=mysql_fetch_array($result)) { ?> <!-- <option value><?=$row['screen']?></option> --> <option value="<?php echo $row['id']; ?>"><?php echo $row['screen']; ?></option> <?php } ?> </select>
Comments
Post a Comment