简体   繁体   English

如何在HTML表单中使用PHP函数检索和显示数据

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

I have been working on this for probably 20 hours of research and trial/error with no solution in sight I figured I'd try here and see if someone can finally point me in the right direction and give me some solid advice. 我已经进行了大约20个小时的研究和试验/错误,但并没有找到解决方案,我想我会在这里尝试,看看是否有人最终可以向我指出正确的方向,并给我一些可靠的建议。

Here's the setup. 这是设置。 I have a page (customer_search.php) that has an HTML form, I want the user to be able to search the DB by $last_name and the results be displayed on a table on the same page. 我有一个具有HTML表单的页面(customer_search.php),我希望用户能够通过$ last_name搜索数据库,并将结果显示在同一页面上的表中。

First: IS THIS POSSIBLE? 第一:这可能吗? I have read so much over the past few days, I doubt myself but then I think it can be done without using Java and using purely PHP/HTML. 在过去的几天里,我读了很多书,我对此表示怀疑,但随后,我认为无需使用Java和使用纯PHP / HTML即可完成此工作。

I also use MVC model. 我也使用MVC模型。

THIS IS THE MAIN PAGE (customer_search.php) 这是主要页面(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'; ?>

THIS IS customer_db.php in the MODEL folder which contains a function that I'd like to use, get_customers_by_last_name($last_name) 这是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;
 }

I apologize for the OVERLOAD of code, but this is my first post on here and I've tried everything I can think of. 我为代码过载而道歉,但这是我在此处的第一篇文章,我已经尝试了所有可以想到的方法。 I just want to search the DB by last name on the html form and then have the results displayed on the table (on the same page) and I don't care if the page has to refresh. 我只想按html表单上的姓氏搜索数据库,然后将结果显示在表上(在同一页面上),并且我不在乎页面是否必须刷新。 I know PHP is server-side and not client so it needs to refresh. 我知道PHP是服务器端而不是客户端,因此需要刷新。 I just can't seem to get the results to actually display in the table, if I could just get the results there, the next step is to SELECT the customer which passes it to the next page customer_display.php but I'll cross that bridge when there. 我似乎似乎无法使结果真正显示在表中,如果可以将结果显示在表中,则下一步是选择将其传递到下一页的客户customer_display.php,但我会越过它在那里的时候桥。 Thanks for your help, and I really am begging for some help/lessons on this. 感谢您的帮助,我真的很想寻求帮助。 I'm desperate!!!! 我很绝望!!!! :( :(

I have had success doing this same thing in the past using JQuery and jTable. 过去,我使用JQuery和jTable成功完成了同样的事情。

This uses AJAX calls which allows you to call PHP scripts without reloading the page. 这使用了AJAX调用,该调用使您无需重新加载页面即可调用PHP脚本。

You can find out everything you need to know here: http://www.jtable.org/ 您可以在这里找到所有需要了解的信息: http : //www.jtable.org/

Hi you did your homework and are correct that the page HAS to be refreshed. 您好,您完成了作业,并且正确地刷新了页面。 Unless you plan to use an Ajax call with javascrcipt ( jQuery for exemple) 除非您计划将Ajax调用与javascrcipt一起使用(例如jQuery

To do what you ask, you have to put the PHP at the beginning. 要执行您要求的操作,必须将PHP放在开头。

You put it in an If() statement that will only be valid if the form has been posted. 您将其放在If()语句中,该语句仅在表单过帐后才有效。

Here is what I would do with your code: 这是我对您的代码的处理方式:

<?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'; ?>

Some crucial advices: 一些重要的建议:

  1. Use Mysqli (or PDO ) library instead of Msql for php 使用Mysqli (或PDO )库代替php的Msql
  2. you should not trust user input data on your search form you can use the janitor.class to sanitize the post. 您不应该信任搜索表单上的用户输入数据,可以使用janitor.class清理帖子。
  3. When you have more time check these links concerning PHP security : Evil User , PHP security , PHP Secure coding 如果您有更多时间,请检查以下有关PHP安全性的链接: 恶意用户PHP安全性PHP安全编码
  4. Oh! 哦! and don't give two elements in the same page the same id Value the purpose of the id is to give each element a unique identifier. 并且不要在同一页面中为两个元素提供相同的ID。ID的目的是为每个元素赋予唯一的标识符。

This is what AJAX is for... asynchronous communication with the server (ie not on a page load). 这就是AJAX用于...与服务器的异步通信(即不在页面加载时)。 Your best bet would probably be to use JQuery or another JS framework, which will make the AJAX calls a snap: $.ajax(); 最好的选择是使用JQuery或其他JS框架,这将使AJAX调用变得轻松: $.ajax(); :

Something like: 就像是:

$.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);
  }
});

Use AJAX to make a call to a PHP object that instantiates your model and returns your results in a JSON string. 使用AJAX对实例化模型并以JSON字符串返回结果的PHP对象进行调用。 Upon receiving the results, use a JavaScript loop to iterate through the JSON and output your table. 收到结果后,使用JavaScript循环遍历JSON并输出表。

I recommend jQuery, which has its own .ajax() function, and it's easy to manipulate the DOM with your result set. 我建议使用jQuery,它具有自己的.ajax()函数,并且可以轻松使用结果集来操作DOM。 Go read the jQuery docs . 去阅读jQuery文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM