如何根据用户输入动态显示数据



我有一个表单(php文件(,允许用户将投资信息添加到数据库中。 用户从下拉列表中选择一项投资(先前输入数据库的投资(,然后输入其他信息并将其提交到数据库。 我希望当用户选择某项投资时,初始日期(数据库中已有的信息(将显示在"初始日期"字段中。 我使用了javascript/ajax,但它不起作用(初始日期字段中没有显示任何内容(。 我使用 https://www.w3schools.com/php/php_ajax_database.asp 的代码作为模型。 附上代码。 另外,使用Jquery会更好吗?谢谢! 以下是更新.php文件:

<!DOCTYPE html>
<head>
<style type="text/css">
table, th, td
{
border:1px solid black;
border-collapse: collapse;
}
</style>
<title>Updates</title>

<script>
function showDate(str) {
if (str == "") {
document.getElementById("initialDate").innerHTML = "";
return;
} else { 
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("initialDate").innerHTML = 
this.responseText;
}
};
xmlhttp.open("GET","get_date.php?q="+str,true);
xmlhttp.send();
}
}
</script>

</head>
<body>
<?php 
include("session.php");
include("navbar.php");
?>
<form style="margin-top: 60px" action="updateDetails2DB.php" method="post">
<h2>Update</h2>
<table>
<tr>
<th>Date of Update</th>
<td><input type="date" name="date" required></td>
</tr>
<tr>
<th>As Of</th>
<td><input type="date" name="" required></td>
</tr>
<tr>
<th>Occupancy %</th>
<td><input type="number" min="0" max="100" name=""></td>
</tr>
<tr>
<th>Investment</th>
<td><select name="investment_name" onchange="showDate(this.value)">
<?php 
$sql = 'SELECT distinct deal_name FROM tbl_deal'; 
$result = mysqli_query($DBconnect, $sql);
if(mysqli_num_rows($result) > 0)
{
echo "<option value=''>Select 
Investment</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='$row[0]'>$row[0] 
</option>";   
}
}
else
{
echo "<option value=''>No Investments 
Found</option>";
}
?>
</select></td>
</tr>
<tr>
<th>Entity Current Value</th>
<td>$<input type="text" name="current_val" size="20" required></td>
</tr>
<tr>
<th>Basis For Valuation</th>
<td><select name="basis_for_valuation">
<option value="NOI">NOI</option>
<option value="Appraisal">Appraisal</option>
<option value="Manager Rep">Manager Rep</option>
<option value="Cap Rate">Cap Rate</option>
<option value="Reduction">Reduction</option>
<option value="Other">Other</option>
</select></td>
</tr>
<tr>
<th>Initial Value</th>
<td></td>
</tr>
<tr>
<th>Initital Date</th>
<td><div id="initialDate"></div></td>
</tr>
<tr>
<th>Average Annual Change in Values</th>
<td></td>
</tr>
</table>
<!--...more entries-->
<input type="submit" name="submit" value="Submit Data" class="btn">
</form>
</body>
</html>

这是get_date.php文件:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
include("config.php");
$q = intval($_GET['q']);
if (!$DBconnect) {
die('Could not connect: ' . mysqli_error($DBconnect));
}
$sql="SELECT deal_date FROM tbl_deal WHERE deal_name = '".$q."'";
$result = mysqli_query($DBconnect,$sql);
while($row = mysqli_fetch_array($result)) {
echo $row['deal_date'];
}
mysqli_close($DBconnect);
?>
</body>
</html>

$DBconnect字符串是 config.php/session.php 中的一个全局变量,它适用于我的所有其他文件/查询。

我发现了这个问题 - 在get_date.php文件中,$q = intval($_GET['q'](,它给出了 q 的整数值。 但是,我正在使用字符串,所以它不起作用。 我取出了intval((并将其替换为$q = $_GET['q'],它有效!

最新更新