簡體   English   中英

如何在HTML表單中使用PHP函數檢索和顯示數據

[英]How to retrieve and display data using a PHP function within an HTML form

我已經進行了大約20個小時的研究和試驗/錯誤,但並沒有找到解決方案,我想我會在這里嘗試,看看是否有人最終可以向我指出正確的方向,並給我一些可靠的建議。

這是設置。 我有一個具有HTML表單的頁面(customer_search.php),我希望用戶能夠通過$ last_name搜索數據庫,並將結果顯示在同一頁面上的表中。

第一:這可能嗎? 在過去的幾天里,我讀了很多書,我對此表示懷疑,但隨后,我認為無需使用Java和使用純PHP / HTML即可完成此工作。

我也使用MVC模型。

這是主要頁面(customer_search.php)

<?php include '../view/header.php';


?>

<div id="main">
        <h1>Customer Search</h1>


            <div id="content">
  <form action="" method="post" id="aligned"

        <label>&nbsp Last Name</label>
        <input type="text" name="last_name"/>
        <br />
        <label>&nbsp;<label>
        <input type="submit" value="Search" />


            </div>
        <div id="content">
      <h2>Results</h2>

        <table>
        <tr>
            <th>Name</th>
            <th>Email Address</th>
            <th>City</th>
            <th>&nbsp;</th>
        </tr>
        <tr>
            <td><?php echo $_POST['firstName'] .' '. $_POST['last_name']; ?></td>
            <td><?php echo $_POST['email']; ?></td>
            <td><?php echo $_POST['city']; ?></td>
            <td><form action="customer_display.php" method="post">
                <input type="hidden" name="action" value="get_customer" />
                <input type="hidden" name="customer_id"
                       value="<?php echo $_POST['customer_ID']; ?>" />
                <input type="submit" value="Select" />

            </form>
            </td>

        </tr>
        </table>
      <br />
 </div>

</div>    
<?php include '../view/footer.php'; ?>

這是MODEL文件夾中的customer_db.php,其中包含我要使用的函數get_customers_by_last_name($ last_name)

<?php
function get_customers() {
global $db;
$query = 'SELECT * FROM customers
            ORDER BY lastName';
$customers = $db->query($query);
    return $customers;
}

function get_customers_by_last_name($last_name) {
global $db;
$query = "SELECT * FROM customers
        WHERE lastName = '$last_name'
        ORDER BY lastName";
$customers = $db->query($query);
return $customers;
 }

我為代碼過載而道歉,但這是我在此處的第一篇文章,我已經嘗試了所有可以想到的方法。 我只想按html表單上的姓氏搜索數據庫,然后將結果顯示在表上(在同一頁面上),並且我不在乎頁面是否必須刷新。 我知道PHP是服務器端而不是客戶端,因此需要刷新。 我似乎似乎無法使結果真正顯示在表中,如果可以將結果顯示在表中,則下一步是選擇將其傳遞到下一頁的客戶customer_display.php,但我會越過它在那里的時候橋。 感謝您的幫助,我真的很想尋求幫助。 我很絕望!!!! :(

過去,我使用JQuery和jTable成功完成了同樣的事情。

這使用了AJAX調用,該調用使您無需重新加載頁面即可調用PHP腳本。

您可以在這里找到所有需要了解的信息: http : //www.jtable.org/

您好,您完成了作業,並且正確地刷新了頁面。 除非您計划將Ajax調用與javascrcipt一起使用(例如jQuery

要執行您要求的操作,必須將PHP放在開頭。

您將其放在If()語句中,該語句僅在表單過帳后才有效。

這是我對您的代碼的處理方式:

<?php
if(isset($_POST['last_name'])){
include_once 'customer_db.php';
$customers = get_customers_by_last_name($_POST['last_name']);
 }
 include '../view/header.php';


?>

<div id="main">
        <h1>Customer Search</h1>


 <div id="Search">
  <form action="" method="post" id="aligned"

        <label>&nbsp Last Name</label>
        <input type="text" name="last_name"/>
        <br />
        <label>&nbsp;<label>
        <input type="submit" value="Search" />
  </form>

</div>
<?php if(isset($customers)){ ?>
 <div id="Results">
      <h2>Results</h2>

        <table>
        <tr>
            <th>Name</th>
            <th>Email Address</th>
            <th>City</th>
            <th>&nbsp;</th>
        </tr>
        <tr>
            <?php while ($a_customer = mysql_fetch_assoc($customer)) { ?>

            <td><?php echo $a_customer['firstName'] .' '. $a_customer['last_name']; ?></td>
            <td><?php echo $a_customer['email']; ?></td>
            <td><?php echo $a_customer['city']; ?></td>
            <td><form action="customer_display.php" method="post">
                <input type="hidden" name="action" value="get_customer" />
                <input type="hidden" name="customer_id"
                       value="<?php $a_customer['customer_ID']; ?>" />
                <input type="submit" value="Select" />

            <?php } ?>
            </td>

        </tr>
        </table>
      <br />
 </div>
<?php }?>

</div>    
<?php include '../view/footer.php'; ?>

一些重要的建議:

  1. 使用Mysqli (或PDO )庫代替php的Msql
  2. 您不應該信任搜索表單上的用戶輸入數據,可以使用janitor.class清理帖子。
  3. 如果您有更多時間,請檢查以下有關PHP安全性的鏈接: 惡意用戶PHP安全性PHP安全編碼
  4. 哦! 並且不要在同一頁面中為兩個元素提供相同的ID。ID的目的是為每個元素賦予唯一的標識符。

這就是AJAX用於...與服務器的異步通信(即不在頁面加載時)。 最好的選擇是使用JQuery或其他JS框架,這將使AJAX調用變得輕松: $.ajax();

就像是:

$.ajax({
  type: "POST",
  url: '/directory/to/php_script_that_outputs_the_table_html.php',
  data: { name: "Smith"},
  success: function(data) {
    $('.class_of_div_to_recieve_code').html(data);
  }
});

使用AJAX對實例化模型並以JSON字符串返回結果的PHP對象進行調用。 收到結果后,使用JavaScript循環遍歷JSON並輸出表。

我建議使用jQuery,它具有自己的.ajax()函數,並且可以輕松使用結果集來操作DOM。 去閱讀jQuery文檔

暫無
暫無

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

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