我有一个表格(php文件),该表格允许用户将投资信息添加到数据库中。用户从下拉列表中选择一项投资(先前输入到数据库中的投资),然后输入其他信息并将其提交给数据库。我希望当用户选择某个投资时,初始日期(数据库中已经存在的信息)将显示在“初始日期”字段中。我使用了javascript / ajax,但无法正常工作(初始日期字段中未显示任何内容)。我将https://www.w3schools.com/php/php_ajax_database.asp处的代码用作模型。随附的代码。另外,使用Jquery会更好吗?谢谢! 这是updates.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中的全局变量,它可以与其他所有文件/查询一起使用。
答案 0 :(得分:0)
我发现了问题-在get_date.php文件中,$ q = intval($ _ GET ['q']),给出q的整数值。但是,我正在使用字符串,所以它不起作用。我拿出intval()并用$ q = $ _GET ['q']代替了它!