【问题标题】:want to fetch conditional data using ajax想要使用 ajax 获取条件数据
【发布时间】:2017-01-02 20:33:53
【问题描述】:

我的工作场景是,我有 4 种笔 1-钻石 2-金 3-青铜 4- 银

我希望当有人在底部输入中选择 Diamond Pen 时,他会输入数量,所以如果他选择 1-Diamond Pen,那么数量应该彼此不同,同样,所有 4 种类型的笔速率应该不同。

我的代码是

ajax.php

<!DOCTYPE html>
<html>
<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 (this.readyState==4 && this.status==200) {
      document.getElementById("txtHint").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>
<!-- <input type="text" name="users" onchange="showUser(this.value)"> -->
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Diamond</option>
<option value="2">Gold</option>
<option value="3">Bronze</option>
<option value="4">Silver</option>
</select>
<!-- <input type="submit" name="users"> -->

</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

getuser.php

<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','','fm_all');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM all_company WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['city'] . "</td>";
    echo "<td>" . $row['province'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

问题只是 Drop Box 值有效,我不知道如何附加输入数量框。请帮忙。

注意:当我选择下拉框值时,此代码根据 id 工作,因此它根据 id 显示,但我没有附加数量。

【问题讨论】:

  • 还有什么问题?添加新的&lt;input&gt;?
  • 现在投递框工作,我想加入一个输入框以获取数量,但问题是我不知道如何加入它
  • "getuser.php?q="+str+"&amp;quanity="+qty

标签: javascript php jquery css ajax


【解决方案1】:

如果您不想对表单提交执行任何操作,您可以通过以下方式实现此目的

在 onkeyup 上调用 showUser() 进行输入并提供一些 id 可能是数量 在选择框的 onchange 上调用 showUser() 并为其提供一些 id 可以是 pen

function showUser(){
    quantity = $("#quantity").val();
    selected_pen = $("#pen").val();
    if (quantity != null && selected_pen != null && typeof(quantity) !='undefined'){
        your ajax calling code
    }
}

您可以为选择框调用上述函数onchange,在输入文本框中调用onkeyup。但是通过上述方式,每当您的文本字段或选择框发生变化时,它都会调用 ajax。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多