简体   繁体   中英

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.

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.

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.

I also use MVC model.

THIS IS THE MAIN PAGE (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)

<?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. I know PHP is server-side and not client so it needs to refresh. 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. 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.

This uses AJAX calls which allows you to call PHP scripts without reloading the page.

You can find out everything you need to know here: 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)

To do what you ask, you have to put the PHP at the beginning.

You put it in an If() statement that will only be valid if the form has been posted.

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
  2. you should not trust user input data on your search form you can use the janitor.class to sanitize the post.
  3. When you have more time check these links concerning PHP security : Evil User , PHP security , PHP Secure coding
  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.

This is what AJAX is for... asynchronous communication with the server (ie not on a page load). Your best bet would probably be to use JQuery or another JS framework, which will make the AJAX calls a snap: $.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. Upon receiving the results, use a JavaScript loop to iterate through the JSON and output your table.

I recommend jQuery, which has its own .ajax() function, and it's easy to manipulate the DOM with your result set. Go read the jQuery docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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