php ve ajax ile açılan

3 Cevap php

For some time I am battling to solve this problem but I am not coming to any conclusion so thought to seek some help here. The problem is that I am getting a blank dropdown instead I should get list of cities populated from the database. Database connection is fine but I am not getting anything in my dropdown.

Bu ben yapıyorum budur:

    <?php

require 'includes/connect.php'; - database connection
$country=$_REQUEST['country']; - get from form (index.php)
$q = "SELECT city FROM city where countryid=".$country;    
$result = $mysqli->query($q) or die(mysqli_error($mysqli));    
if ($result) {
?>  
    <select name="city">
    <option>Select City</option>
    $id = 0;
    <?php while ($row = $result->fetch_object()) { 
       $src = $row->city;
       $id = $id + 1;      
    ?>      
    <option value= <?php $id ?> > <?php $src ?></option>
<?php } ?>
</select>
<?php } ?>

ajax komut şudur:

    <script>
    function getXMLHTTP() { //function to return the xml http object
            var xmlhttp=false;  
            try{mlhttp=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 getCity(strURL) {          
            var req = getXMLHTTP();         
            if (req) {              
                req.onreadystatechange = function() {
                    if (req.readyState == 4) {                      
                        if (req.status == 200) {                        
                            document.getElementById('citydiv').innerHTML=req.responseText;  

                        } else {
                            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                        }
                    }               
                }           
                req.open("GET", strURL, true);

                req.send(null);

            }

        }
    </script>

Bu benim form kodu:

<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150">Country</td>
    <td  width="150"><select name="country" onChange="getCity('findcity.php?country='+this.value)">
    <option value="">Select Country</option>
    <option value="1">New Zealand</option>
    <option value="2">Canada</option>
        </select></td>
  </tr>
  <tr style="">
    <td>City</td>
    <td ><div id="citydiv"><select name="city">
    <option>Select City</option>
        </select></div></td>
  </tr>

</table>
</form>

3 Cevap

Sana <option> etiketler çıkış nerede sorun olduğunu düşünüyorum.

Lütfen <select> etiketleri arasında kod bloğunu kullanmayı deneyin.

    <option>Select City</option>
    <?php 
       $id = 0;
       while ($row = $result->fetch_object()) { 
       $src = $row->city;
       $id = $id + 1;      
    ?>          
    <option value="<?php echo htmlspecialchars($id,ENT_QUOTES) ?>"><?php echo htmlspecialchars($src) ?></option>
<?php } ?>

Düzenleme: açıklığa kavuşturmak için, $id ve $src değişkenleri önce herhangi bir echo ifadeleri yoktu. I htmlspecialchars() düzgün html kaçtı üretmek için bir alışkanlık olarak ekledi.

Denemek için bir kaç şey:

  1. Eğer veritabanında var biliyorum bir şehir ile tarayıcınızda elle findcity.php talep ederseniz, ben doğru HTML dönecektir?

  2. Onreadystatechange fonksiyonu bir kesme noktası ayarlamak ve beklendiği gibi döndürülen değerler olup olmadığını görmek için, Kundakçı veya başka bir javascript debugger ile deneyin. Fonksiyonunun ilk satırında kesme noktası ayarlayın.

<input name="acname" type="text" id="acname" value="" maxlength="9">
Account Name </p>

<select name="src">
  <option value="number"> :::: Select ::::</option>
  <option value="did">DID</option>
  <option value="tfn">TFN</option>
</select>
<span id="errmsg"></span> DID/TFN </p>

<select name="did" onchange='OnChange(this.form1.did);' >
  <option value=""> :::: Select ::::</option>
  <? $qry1 = mysql_query("SELECT * FROM ".NUMBERS." where Flag='1'") or die(mysql_error()) ;
     while($res1 = mysql_fetch_array($qry1)) {  ?>
     <option value="<?=$res1['Numbers'] ?>"><?=$res1["Numbers"]?></option>
      <? } ?>
</select>

SOURCE