簡體   English   中英

如何根據用戶輸入動態顯示數據

[英]how to dynamically display data based on user input

我有一個表格(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中的全局變量,它可用於所有其他文件/查詢。

我發現了問題-在get_date.php文件中,$ q = intval($ _ GET ['q']),它給出q的整數值。 但是,我正在使用字符串,所以它不起作用。 我取出intval()並替換為$ q = $ _GET ['q'],它可以正常工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM