php - 如何在 PHP 和 Mysql 中将字符串值传递给 ajax

标签 php mysql ajax

我只需要在下拉列表中显示不同的值。下拉列表选择基于 ajax 中的结果校准,并在下拉列表中显示下面显示的相关数据。

如何在此脚本中将字符串值传递给ajax。

有人请指导我

<!doctype html>
<html class="no-js" lang="en">
  <head>
    <script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>
  </head>
  <body>

    <div class="row">
      <div class="large-12 columns">
        <h1></h1>
      </div>
    </div>

    <div class="row">
      <div class="large-12 columns">
        <!--Main Tab Start-->
        <?php
        $res=mysql_query("select DISTINCT tag,id from password");

        if($res === false )
        {
            die(mysql_error());
        }
        ?>
        <form action="" method="post">
            <select name="tagg" onchange="showUser(this.value)">
            <option value="">Select Accounts</option>
        <?php
        while($row=mysql_fetch_array($res))
        {
            $dess=$row['tag'];
            $id=$row['id'];
            ?>
            <option value="<?php echo $id;?>"><?php echo $dess; ?></option>

            <?php
        }

        ?>
                    </select>
            </form>
             <div id="txtHint"><b>Person info will be listed here.</b></div>
        <!--Main Tab Ent-->
      </div>
    </div>
  </body>
</html>

Getuser.php

<?php
$q = intval($_GET['q']);
$result=mysql_query("select * from password WHERE id = '".$q."'");
if($result === false )
{
    die(mysql_error());
}
while($row=mysql_fetch_array($result))
{
    $desss=$row['tag'];
}
$res=mysql_query("select * from password WHERE tag = '".$desss."'");
if($res === false )
{
    die(mysql_error());
}
?>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>username</th>
<th>Password</th>
<th>Description</th>
<th>Link</th>
</tr>
</thead>
<?php

while($row=mysql_fetch_array($res))
{
    $id=$row['id'];
     $name=$row['name'];
    $url=$row['url'];
    $uname=$row['username'];
    $pass=$row['password'];
    $tag=$row['tag'];
    $des=$row['description'];

    ?>
    <tbody>
    <tr>
    <td><?php echo $id; ?></td>
    <td><?php echo $name; ?></td>
    <td><?php echo $uname; ?></td>
    <td><span data-tooltip aria-haspopup="true" class="has-tip" title="<?php echo $pass; ?>">View</span></td>
    <td><span data-tooltip aria-haspopup="true" class="has-tip" title="<?php echo $des; ?>">Description</span></td>
    <td><a href="<?php echo $url; ?>" title="<?php echo $name; ?>" target="_blank">Link</a></td>

    </tr>
    </tbody>
    <?php

}
?>
</table>

最佳答案

我已经运行了您的代码,它运行良好。只有一个问题,如果您想传递字符串,请更改您的 gestuser.php 中的以下代码 改变

$q = intval($_GET['q']);

$q =($_GET['q']);

和改变

<option value="<?php echo $id;?>"><?php echo $dess; ?></option>

<option value="<?php echo $dess;?>"><?php echo $dess; ?></option>

否则你的代码是有效的

关于php - 如何在 PHP 和 Mysql 中将字符串值传递给 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29692098/

相关文章:

jquery - 在 url 中传递多个类似哈希的参数

php - 如何在 Yii2 GridView 中使用多选下拉列表执行筛选

mysql - 更新具有计数条件要求的属性值

javascript - 列出选定类别和价格范围内的产品

php - 如何让 mysqli 实例在函数中被识别?

java - 从单独的类运行 mysql 函数

c# - 验证域名

php - 这个 Ajax 可以与调试(Firebug)一起使用,但也不能没有它

php - 拉拉维尔 5.5 : MongoDB & hasManyThrough() relation

php - 使用单个 PDO 查询意外达到 PHP 内存限制?