-1

How can i make a search from the form in an HTML page display the result in the same html page? Am having an error when i try and run the code that says inputPackageID is not defined. How can i get to solve this. Seems like the variable is not being recognized from the form. Please help

Here is my code. If you dont understand my question you can ask me where you are not clear.

<div class="main">
    <div class="col-md-12">
        <div class="row">
            <div class="container">

        </div>

        <div class="search center-block col-md-6" align="center">
        <p>
            Want to track your package? <br />
            Enter your Package ID(PID)
        </p>
        <form method="post" action="trackShipment.php">
        <div class="form-group">

            <label class="sr-only" for="inputPackageID"></label>
            <input type="search" name="inputPackageID" class="form-control edit-form-control" id="inputPackageID" placeholder="PID">
        </div>
        <div class="modal-footer">
          <input type="submit" class="btn btn-primary" name="submit" />
        </div>
        </form>
    </div>


        <div class="col-md-8" align="center">
        <h2>Well, Here is your package!</h2>
                    <?php
                        include '../includes/connection.php';
                        //include 'phpscripts/trackShipment.php';
                        $PackageID = $_GET['inputPackageID'];

                        $sql = "select * from package where p_id='$PackageID'";

?>






            <table class="table table-striped">
                <tr>
                    <th><span>PID</span></th>
                    <th><span>Customer ID</span></th>
                    <th>Name</th>
                    <th>Description</th>
                    <th>Destination Per KM</th>
                    <th>Type</th>
                    <th>Price/KG</th>
                </tr>
                </th>
                <?php 

                     foreach ($db->query($sql) as $count)?>

                   <tr>
                        <td>
                            <?php echo $count['p_ID'];  ?>
                        </td>
                        <td>
                            <?php echo $count['cus_ID']; ?>
                        </td>
                        <td>
                            <?php echo $count['package_name']; ?>
                        </td>
                        <td>
                            <?php echo $count['package_description']; ?>
                        </td>
                        <td>
                            <?php echo $count['package_type']; ?>
                        </td>
                        <td>
                            <?php echo $count['destination_per_km']; ?>
                        </td>
                        <td>
                            <?php echo $count['price_per_kg']; ?>
                        </td>
                <?php } ?>
            </table>


    </div>
</div>

Willie Mwewa
  • 341
  • 3
  • 13
  • Is the page you posted the code from named "trackShipment.php"? If so, then it should be submitting to itself. However, your title question doesn't match the question in the body. In the title you ask "how to search a specific row in a database", but in the body you ask how to make a search form display the result on the same page. These are two different questions. Can you please clarify? – Beems May 11 '15 at 20:55
  • You are vulnerable to [sql injection attacks](http://bobby-tables.com). And the variable's not recognized, because the first time you hit this form's page, there's no `?inputPackageID=xxx` in the url. – Marc B May 11 '15 at 20:57
  • @Beems sorry for that. I have had areally long day and its late here. I am trying to solve whats in the body of the question. And trackShipment.php is another php file not the page am currently working on. – Willie Mwewa May 11 '15 at 21:01
  • `
    ` then use `action=""` if you want results on the same page or Ajax, as outlined in my answer below. If that doesn't solve it and with what I explained in my answer about it being undefined, then I'll simply delete it. @WillieMwewa
    – Funk Forty Niner May 11 '15 at 21:10

1 Answers1

2

It's showing you that it's not defined because your form uses a post method, and you're using a GET array.

$PackageID = $_GET['inputPackageID'];

change that to

$PackageID = $_POST['inputPackageID'];

Plus, use isset()

if(isset($_POST['inputPackageID'])){
    $PackageID = $_POST['inputPackageID'];
}

since you're using your entired code inside the same page.

or !empty()

if(!empty($_POST['inputPackageID'])){
    $PackageID = $_POST['inputPackageID'];
}

If you're using the same code inside the same file, you can use action="" and an extra conditional statement for your submit button.

if(isset($_POST['submit'])){...}

Seeing a comment of yours, this isn't the case then and you are using a seperate file, so we can scratch that.

However, if a GET method is what you really want to use, you will need to change your method to method="get" in your form. Using this method is unsafe, consult my footnotes.


Footnotes:

Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.


"How can i make a search from the form in an HTML page display the result in the same html page?"

  • You can either use Ajax for this, or use action="".
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141