mysql - PHP, Uncaught TypeError: Cannot read property 'length' of undefined -
i'm trying make dependable dropdown lists on php ajax calls. there 9 columns on mysql table. found sample code. there 3 dropdown lists. , tried increase them.
<!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <title></title> <meta name="description" content=""> <meta name="keywords" content=""> <script type="text/javascript"> function ajaxfunction(choice) { { alert(choice.options.length);} var httpxml; try { // firefox, opera 8.0+, safari httpxml=new xmlhttprequest(); } catch (e) { // internet explorer try { httpxml=new activexobject("msxml2.xmlhttp"); } catch (e) { try { httpxml=new activexobject("microsoft.xmlhttp"); } catch (e) { alert("your browser not support ajax!"); return false; } } } function statechanged() { if(httpxml.readystate==4) { //alert(httpxml.responsetext); var myobject = json.parse(httpxml.responsetext); for(j=document.myform.state.options.length-1;j>=0;j--) { document.myform.state.remove(j); } var state1=myobject.value.state1; var optn = document.createelement("option"); optn.text = 'select state'; optn.value = ''; document.myform.state.options.add(optn); (i=0;i<myobject.state.length;i++) { var optn = document.createelement("option"); optn.text = myobject.state[i]; optn.value = myobject.state[i]; document.myform.state.options.add(optn); if(optn.value==state1){ var k= i+1; document.myform.state.options[k].selected=true; } } ////////////////////////// for(j=document.myform.city.options.length-1;j>=0;j--) { document.myform.city.remove(j); } var city1=myobject.value.city1; //alert(city1); (i=0;i<myobject.city.length;i++) { var optn = document.createelement("option"); optn.text = myobject.city[i]; optn.value = myobject.city[i]; document.myform.city.options.add(optn); if(optn.value==city1){ document.myform.city.options[i].selected=true; } } //////////////////// for(j=document.myform.country.options.length-1;j>=0;j--) { document.myform.country.remove(j); } var name1=myobject.value.name1; var optn = document.createelement("option"); optn.text = 'select name'; optn.value = ''; document.myform.country.options.add(optn); (i=0;i<myobject.name.length;i++) { var optn = document.createelement("option"); optn.text = myobject.name[i]; optn.value = myobject.name[i]; document.myform.country.options.add(optn); if(optn.value==name1){ var k= i+1; document.myform.name.options[k].selected=true; } } /////////////////////////// document.getelementbyid("txthint").style.background='#00f040'; document.getelementbyid("txthint").innerhtml='done'; //settimeout("document.getelementbyid('txthint').style.display='none'",3000) } } var url="ajax-dd3ck.php"; var country=myform.country.value; if(choice != 's1'){ var state=myform.state.value; var city=myform.city.value; var name=myform.name.value; }else{ var state=''; var city=''; var name=''; } url=url+"?country="+country; url=url+"&state="+state; url=url+"&city="+city; url=url+"&name="+name; url=url+"&id="+math.random(); myform.st.value=state; //alert(url); document.getelementbyid("txthint2").innerhtml=url; httpxml.onreadystatechange=statechanged; httpxml.open("get",url,true); httpxml.send(null); document.getelementbyid("txthint").innerhtml="please wait...."; document.getelementbyid("txthint").style.background='#f1f1f1'; } </script> </head> <body > </head> <body> <div id="txthint" style="width : 100px;background-color: #cccc33;">message area</div> <br><br> <form name="myform" action='ajax-dd3-details.php' method='post'"> <input type=hidden name=st value=0> <table width=500> <tr><td > select country<br><select name="country" id="s1" onchange="ajaxfunction(this);"> <option value=''>select one</option> <?php //require "../include/z_db1.php"; require "config.php";// connection database $sql="select distinct country student5 "; foreach ($dbo->query($sql) $row) { echo "<option value=$row[country]>$row[country]</option>"; } ?> </select> </td><td ><select name=state id="s2" onchange="ajaxfunction(this)";> <option value=''>select one</option></select></td> <td ><select name=city id="s3" onchange="ajaxfunction(this);"> <option value=''>select one</option></select></td> <td ><select name=sex id="s4" onchange="ajaxfunction(this);"> <option value=''>select one</option></select></td> </tr></tr> <tr><td colspan=3><input type=submit value='submit'></td></tr> </form> </table> <br><br> <div id="txthint2"></div> </body> </html>
i edited php file , added below code. because want add name column mysql dropdown:
//////////////////// for(j=document.myform.name.options.length-1;j>=0;j--) { document.myform.name.remove(j); } var name1=myobject.value.name1; var optn = document.createelement("option"); optn.text = 'select name'; optn.value = ''; document.myform.name.options.add(optn); (i=0;i<myobject.name.length;i++) { var optn = document.createelement("option"); optn.text = myobject.name[i]; optn.value = myobject.name[i]; document.myform.name.options.add(optn); if(optn.value==name1){ var k= i+1; document.myform.name.options[k].selected=true; } }
i edited other php file:
<?php require "config.php"; // connection details error_reporting(0);// no error reporting there ////////// ///////////////////////////////////////////////////////////////////////////// $country=$_get['country'];// //$country='ind'; // check can use $state1=$_get['state']; $city1=$_get['city']; $name1=$_get['name']; //$state1="andhra pradesh"; ///////////// validate inputs //////////// // checking country variable /// if((strlen($country)) > 0 , (!ctype_alpha($country))){ echo "data error"; exit; } // checking state variable (with space ) /// if ((strlen($state1)) > 0 , ctype_alpha(str_replace(' ', '', $state1)) === false) { echo "data error"; exit; } // checking class variable /// if((strlen($name1)) > 0 , (!ctype_alpha($name1))){ echo "data error"; exit; } /////////// end of input validation ////// if(strlen($country) > 0){ $q_country="select distinct(state) student5 country = '$country'"; }else{ $q_country="select distinct(state) student5"; } //echo $q_country; $sth = $dbo->prepare($q_country); $sth->execute(); $state = $sth->fetchall(pdo::fetch_column); $q_state="select distinct(city) student5 "; if(strlen($country) > 0){ $q_state= $q_state . " country = '$country' "; } if(strlen($state1) > 0){$q_state= $q_state . " , state='$state1'";} $sth = $dbo->prepare($q_state); $sth->execute(); $city = $sth->fetchall(pdo::fetch_column); $q_name="select distinct(name) student5 "; if(strlen($country) > 0){ $q_state= $q_state . " country = '$country' "; } if(strlen($state1) > 0){$q_state= $q_state . " , state='$state1'";} if(strlen($city1) > 0){$q_city= $q_city . " , city='$city1'";} $sth = $dbo->prepare($q_city); $sth->execute(); $name = $sth->fetchall(pdo::fetch_column); $main = array('state'=>$state,'city'=>$city,'name'=>$name1, 'value'=>array("state1"=>"$state1","city1"=>"$city1", "name"=>"$name1")); echo json_encode($main); ////////////end of script ///////////////////////////////////////////////////////////////////////////////// ?>
i added code above:
$q_name="select distinct(name) student5 "; if(strlen($country) > 0){ $q_state= $q_state . " country = '$country' "; } if(strlen($state1) > 0){$q_state= $q_state . " , state='$state1'";} if(strlen($city1) > 0){$q_city= $q_city . " , city='$city1'";} $sth = $dbo->prepare($q_city); $sth->execute(); $name = $sth->fetchall(pdo::fetch_column);
when open via lamp, new dropdown list doesn't work. , i got error. can fix this?
edit: and here mean "name"
document.myform.name.options.length
giving error. have use name
of select
options
length
. use document.myform.country.options.length
so in scenario, have this.
in html
<select name=country id='s1' onchange=ajaxfunction(this);>
and in js
function ajaxfunction(choice) { alert(choice.options.length);}
Comments
Post a Comment