php - 根据两个下拉选择值获取索引页面中的图像和其他详细信息?

标签 php javascript ajax

我是 php 新手。我对下拉列表有疑问。让我详细解释一下。我有一个包含两个表的数据库:firstCata 和 subCata。表 subCata 引用 firstCata ID。现在在我的 index.php 页面中,我有一个带有 2 个下拉列表的表单。第一个用于 firstCata,第二个显示基于我数据库中第一个的值。我使用 Ajax.Code 完成的所有这些都在下面。问题是使用 ajax 我在 index.php 中的标签上显示了第二个下拉列表。如何在 index.php 页面中显示 subCata 的其他详细信息?

<head>
<script type="text/javascript">
function getSubCata(str)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("result").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getSunCata.php?q="+str.value,true);
xmlhttp.send();
}
</script>
</head>
<select onchange='getSubCata(this)'>
<?php 
$query=mysql_query("SELECT * FROM firstCata") or die(mysql_error());
while($rec=mysql_fetch_array($query))
{
?>
<option value="<?php echo $rec['firstCata_id'];?>"><?php echo $rec['firstCata_name'];?></option>
<?php } ?>
</select>

我的 getSunCata.php 代码就像

<?php
mysql_connect('localhost','root','');
mysql_select_db('mainCata') or die(mysql_error());

?>
<select>
<?php
$id=$_GET['q'];
echo $id;
$query=mysql_query("SELECT * FROM subcata where firstCata_id=$id") or die(mysql_error());
while($rec=mysql_fetch_array($query))
{
echo $rec['subCata_name'];
?>
 <option id="a"value="echo $rec['subCata_id'];">
    <?php echo $rec['subCata_name']; ?>
</option>
    <?php
}
?>
</select>

最佳答案

如果我理解正确,您需要在第二次选择中打印所有 subCata 数据。 那么,如果您还想显示所有 subCata 值,为什么还需要 ajax 呢? 这是我的解决方案:

// MySQL query
$sql = '
    SELECT
        *
    FROM
        firstCata
    JOIN
        subCata
    ON
        firstCata_id = subCata_id';

使用 jQuery 模板 html 脚本

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    var selects_pair = $('select[name="firstCata"], select[name="subCata"]')
    selects_pair.change(function(){
        // get selected id
        var selected_id = $(':selected', this).val();
        // set another selectd corresponding value
        $('[value="' + selected_id + '"]', selects_pair.not(this)).attr("selected", "selected");
    });
});
</script>

模板 html 标记

// looping through the data like this, add foreach cycle as you want

<select name="firstCata">
    <option value="<?php echo $rec['firstCata_id'];?>"><?php echo $rec['firstCata_name'];?></option>
</select>

<select name="subCata">
    <option value="<?php echo $rec['subCata_id'];?>"><?php echo $rec['subCata_name'];?></option>
</select>

关于php - 根据两个下拉选择值获取索引页面中的图像和其他详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13008719/

相关文章:

javascript - 如何在更改 div 内容时添加淡入淡出效果?

Javascript 无法识别使用 XMLHttpRequest 输入到 DIV 中的项目

javascript - 如何使用 javascript 禁用除按钮之外的所有表单字段

php - Dockerfile使用mysqli和a2enmod重写启动PHP容器不起作用

php - 返回多于 1 行的子查询

javascript - jquery ui Accordion 禁用点击

使用 Codeigniter 和 jQuery 表单插件上传 Ajax 文件

ajax - Telerik 网格在删除命令后显示空列

javascript - 如何使带有2个值的ajax 'data'分别获取每个值?

php - 这在文档 : square bracket followed by comma ( [, 中意味着什么)