我有一个从数据库填充的select元素,当我选择一个元素时,我想向我展示与该行相关的另一列的其他数据。
例如: 在下拉列表中我有: 苹果, 香蕉, 梨
当我选择苹果时,我想从数据库中选择列'总计'这告诉我我剩下多少苹果。
我想,我应该使用Javascript-Ajax?或者是用PHP做另外一种方式。
提前致谢!
<?php include("db.php"); ?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$('select').material_select();
});
function myFunction() {
if (document.getElementById('selectid').value == "other") {
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
x.setAttribute("placeholder", "Write another");
x.setAttribute("class", "input-field col s12");
document.getElementById("myDIV").appendChild(x);
}
}
</script>
</head>
<body>
<div id="myDIV" class="input-field col s4">
<?php
$sql12 = "select * from fruitswhere total>0 && spent=1";
$res1 = mysqli_query($conn, $sql12) or die("Error");
$select = '<select id="selectid" name="name" onchange="myFunction()"><option value = "" disabled selected> Choose </option>';
while($row1 = mysqli_fetch_assoc($res1)) {
$emri = $row1['name'];
// $select.='<option value = "'.$row1['name'].'">'.$row1['name '].'</option>';
$select .= '<option id="my_option" value="'.$name.'">'.$name.'</option>';
}
$select .= '<option value="other">other</option>';
$select .= '</select>';
echo $select;
?>
</div>
<div class="input-field col s4">
<?php
$query2 = "SELECT total FROM fruits WHERE name='$name'";
$res2 = mysqli_query($conn, $query2);
while($row2=mysqli_fetch_assoc($res2)){
$all = $row2['total'];
}
?>
<input name="total" type="number" min="1">
</div>
</body>
</html>
答案 0 :(得分:1)
您可以设置一个简单的脚本来响应请求。例如,您可以创建fruitCount.php:
<?php
include('db.php');
$sql = 'SELECT total '.
'FROM fruits '.
'WHERE name="'.mysqli_real_escape_string($_POST['name']).'"';
$result = mysqli_query($conn, $sql);
if ($result != NULL) {
$row = mysqli_fetch_assoc($result);
print($row['total']);
} else {
print(0);
}
die();
然后在页面上,您可以使用jQuery设置请求来自此资源的数据的click事件:
$('#selectid').change(function(){
$.post('fruitCount.php', // Server target for request
{ name: $(this).val() }, // POST payload
function(resp){ // Handle response from server
$('#myDiv').html(resp); // Populate value to page
});
});
这是一种允许在服务器端正在发生变化的环境中使用最新值的实现。如果您只需要加载时的值,可以在OPTION元素上设置data-count属性并从中更新#myDiv:
$('#selectid').change(function(){
$('#myDiv').html($(this).data('count'));
});
如何实施它取决于您希望数据的实时性。
答案 1 :(得分:0)
您好我找到了一个使用AJAX的解决方案,在这里,它完美无缺!
<script language = "javascript" type = "text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('result');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var fruitName= document.getElementById('selectid').value;
var queryString = "?name=" + fruitName;
ajaxRequest.open("GET", "ajax_totali.php" + queryString,
true);
ajaxRequest.send(null);
}
}
</script>
<body>
<div id="myDIV" class="input-field col s4">
<?php $sql12="select * from fruits";
$res1=mysqli_query($conn,$sql12) or die( "Error");
$select= '<select id="selectid" name="name onchange="ajaxFunction()"><option value="" disabled selected>Choose one fruit</option>';
while ($row1=mysqli_fetch_assoc($res1)){
$select.='<option value="'.$row1['name'].'">'.$row1['name'].'</option>';
}
$select.='</select>';
echo $select; ?>
</div>
<div class="input-field col s4">
<div id='result'></div>
</div>
</body>
</html>
ajax_total.php
<?php
include('db.php');
$sql = 'SELECT total '.
'FROM fruits'.
'WHERE name="'.$_GET['name'].'"';
$result = mysqli_query($conn, $sql);
if ($result != NULL) {
$row = mysqli_fetch_assoc($result);
// print($row['total']);
$result=$row['total'];
echo "<div style='color:red'>.$result.</div>";
} else {
print(0);
}
die();
?>