0

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!!!! :(

3
  • 1
    By "Java" do you mean JavaScript? It could be done with just PHP/MySQL but it wouldn't be as pretty. It would require a page reload. Commented Dec 11, 2012 at 4:54
  • Its so easy if u can try - datatables.net/examples Commented Dec 11, 2012 at 5:10
  • Doing this without JavaScript you have to repost to the same page. i posted an answer for you on how you could do it Commented Dec 11, 2012 at 5:26

4 Answers 4

2

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.
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, more along the lines of what I was looking for (sticking with just PHP). I appreciate the extra tips and maybe that has something to do with this error I'm now getting. Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\xampp\htdocs\project_start\tech_support\customer_manager\customer_search.php on line 41 Name Email Address City
In the function echo the $query to see what query it is using.
Also erfy the result of the query ther seems to be a database error
Also try using the id instead : <input type="text" id="last_name"/>
2

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/

Comments

0

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.

4 Comments

I have little knowledge of jQuery and Java. So is it not possible to search with the form and just echo the results or using <?php foreach ?> ???
If you want something simple and ugly you can do a standard POST back to the the same page. In PHP you can detect that the size of the $_POST global is greater than zero and iterate through, and display the contents. It's old school and pretty ugly though. Seriously, learning jQuery is worth the effort. It makes JavaScript DOM handling a lot simpler.
I'm going to start learning jQuery soon, just getting started on my programming ventures and PHP is one of the first languages I've started with in web development. Quick question, is what I'm asking relatively easy to do with jQuery? Or would it require more advanced jQuery knowledge?
If you're ready for MVC then I don't see jQuery being too much of a problem. AJAX is easy once you get how it works. Essentially, it's an asynchronous call to another PHP thread in the background. It returns a result and you can then display it. Getting used to handling JSON is also very useful. It's a way to serialize complex data structures (like an array) and it's a native JavaScript data type.
0

This is what AJAX is for... asynchronous communication with the server (i.e. 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);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.