问题与阿贾克斯,jQuery和PHP和两个下拉列表 [英] Issue with Ajax, Jquery and PHP and Two Dropdown list

查看:93
本文介绍了问题与阿贾克斯,jQuery和PHP和两个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充的基础上使用Ajax,jQuery的,PHP和MySQL的第一个下拉列表中选择第二个下拉列表。问题是在第二个下拉列表中的选项只出现在同一行!我希望在results.php内while循环(在一行中的所有选项)可以处理这一点,但它似乎不是!可否请您让我知道我怎么能解决这个问题? 这里是我的code:

 < HTML>
<身体GT;
< PHP
$库MySQLi =新库MySQLi('本地主机','根','','链');
如果($ mysqli-> connect_errno)
{
死亡(无法连接!');
}
其他
{
$查询=SELECT * FROM汽车公司;
如果($结果= $ mysqli->查询($查询))
 {
 如果($ result-> NUM_ROWS大于0)
    {
 ?>
 &其中p为H.;
  选择汽车
<选择ID =selectCar>
<期权价值=选择>请从列表中选择< /选项>
    < PHP
     而($行= $ result-> FETCH_ASSOC())
   {
   ?>
   <期权价值=<?PHP的回声$行['身份证'];?>>< PHP的echo $行['标题']; ?>< /选项>
  < PHP
}
  ?>
 < /选择>
 &所述; / P>
<选择>
    <期权价值=选择>请从列表中选择< /选项>
    <选择ID =结果>< /选项>
    < /选择>
    < PHP
    }
    其他
    {
        回声没有找到记录!;
    }
    $ result->关闭();
}
其他
{
    回声错误查询:$查询。 '$ mysqli->错误;
}
    }
     $ mysqli->关闭();
    ?>
    <脚本类型=文/ JavaScript的SRC =jquery.js和>< / SCRIPT>
    <脚本类型=文/ JavaScript的>
        $(文件)。就绪(函数()
        {
            $('#selectCar')。改变(函数()
            {
                如果($(本).VAL()=='')的回报;
                $获得(
                    results.php,
                    {ID:$(本).VAL()},
                    功能(数据)
                    {
                        $('#结果)HTML(数据)。
                    }
                );
            });
        });
    < / SCRIPT>
      < /身体GT;
      < / HTML>
 

在PHP的结果我有:

 < PHP
 $库MySQLi =新库MySQLi('本地主机','根','','链');
 $ resultStr ='';
 $查询=SELECT * FROM车型WHERE carID ='$ _ GET ['身份证']。
 如果($结果= $ mysqli->查询($查询))
 {
如果($ result-> NUM_ROWS大于0)
{
    而($行= $ result-> FETCH_ASSOC())
    {
  $ resultStr ='<期权价值='&GT'$行['身份证']。;$行['模型']'< /选项>'。;
    }
}
其他
{
 $ resultStr ='没有发现';
}
   }
    回声$ resultStr;
   ?>
 

解决方案

您应该编辑你的脚本到这个..

 <选择ID =结果>
  <期权价值=选择>请从列表中选择< /选项>
< /选择>
 

I am trying to populate second dropdown list based on first dropdown list selection using Ajax, jQuery,PHP and MySQL. the problem is the options in second dropdown list just appears in one line!(all options in one line ) I was hoping the while loop inside the results.php could handle this but it seems not! Can you please let me know how I can fix this issue? and Here is my code:

<html>
<body>
<?php
$mysqli = new mysqli('localhost', 'root', '', 'chain');
if ($mysqli->connect_errno) 
{
die('Unable to connect!');
}
else
{
$query = 'SELECT * FROM Cars';
if ($result = $mysqli->query($query)) 
 {
 if ($result->num_rows > 0) 
    {
 ?> 
 <p>
  Select a Car
<select id="selectCar">
<option value="select">Please Select From The List</option>
    <?php       
     while($row = $result->fetch_assoc()) 
   {
   ?>       
   <option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?></option>      
  <?php         
}
  ?>
 </select>
 </p>
<select>
    <option value="select">Please Select From The List</option>
    <option id="result"></option>
    </select>
    <?php
    }
    else 
    {
        echo 'No records found!';
    }
    $result->close();
}
else 
{
    echo 'Error in query: $query. '.$mysqli->error;
}
    }
     $mysqli->close();
    ?>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function()
        {
            $('#selectCar').change(function()
            {
                if($(this).val() == '') return;
                $.get(
                    'results.php',
                    { id : $(this).val() },
                    function(data)
                    {
                        $('#result').html(data);
                    }
                );
            });
        });
    </script>
      </body>
      </html>

in the PHP result I have:

<?php
 $mysqli = new mysqli('localhost', 'root', '', 'chain');
 $resultStr = '';
 $query = 'SELECT * FROM models WHERE carID='.$_GET['id'];
 if ($result = $mysqli->query($query)) 
 {
if ($result->num_rows > 0) 
{
    while($row = $result->fetch_assoc())
    {
  $resultStr.=  '<option    value="'.$row['id'].'">'.$row['model'].'</option>'; 
    }
}
else
{
 $resultStr = 'Nothing found';
}
   }
    echo $resultStr;
   ?>

解决方案

You should edit your script into this..

<select id="result">
  <option value="select">Please Select From The List</option>
</select>

这篇关于问题与阿贾克斯,jQuery和PHP和两个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆