-3

I have a page where there is search field. the page is designed to have the result on the same page with an id when the submit link is clicked the results will appear below. The submit button is a link not the traditional submit buttons. My problem is i have done all the php stuff but when i click on the link that would let the result appear, the php doesn't run. Could someone please help me out. below is the code. Thanks

    <form action="" method="post">
                        <div class="col-md-9">
                        <div class="row">
                            <div class="col-md-7 no-padding">
                                <div class="inner">
                                    <h3>Track &amp; Trace</h3>
                                    <span>Already have a load ID, please insert it below</span>
                                </div>
                            </div>
                            <div class="col-md-5 no-padding">
                                <input class="input-fullwidth" name="track" id="track">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <a id="track-it" href="#section-tracking-result" class="btn-custom btn-fullwidth">Track it</a>
                    </div>
                </form>
<?php
     define('DB_HOST','localhost');
     define('DB_USER','username');
     define('DB_PASS','password');
     define('DB_NAME','dbname');

     $conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
     mysql_select_db(dbname) or die(mysql_error());
     $output = '';
     if(isset($_POST['track'])) {
         if(empty($_POST['track'])) {
             echo "Tracking Code is Required";
         }
        $searchq = $_POST['track'];
        $searchq = preg_replace("#[^0-9a-z]#i", "",$searchq);
        $query = mysql_query("SELECT * FROM track WHERE keywords LIKE '%$searchq%'");
        $count = mysql_num_rows($query);

        if($count == 0) {
             $output = 'There was no search results!';
        } else {
            while($row = mysql_fetch_array($query)) {
                      $consignee = $row['consignee'];
                      $desti = $row['desti'];
                      $date = $row['date'];
                      $id = $row['id'];
            }
        }


    }

?>
KofYebs
  • 3
  • 5
  • It doesn't seem like you even have
    tags for HTML! Make sure you are using them so PHP can make use of the HTML inputs! Let me know if there are any issues.
    – Lachie Mar 15 '16 at 11:30
  • Use form as usual; create a link for submitting form and use onclick="form.submit();" on link – Sinto Mar 15 '16 at 11:31

2 Answers2

0

You need to put your html inside a form and you just need to style the traditional submit button.

If you don't want to create a form, you'll probably be able to achieve whatever you're doing here in Javascript.

But PHP needs you to submit using a <form></form>

Try this:

<form method="post"> <input type="text" class="input-fullwidth" name="track" id="track" /> <input type="submit" id="track-it" name="submit" class="btn-custom btn-fullwidth" /> </form>

and in your PHP check if it is submitted

if(isset($_POST['submit'])) {
    // your checks and execution goes here
}

This way you check if form is submitted and if the track input is empty then you say that it should not be.

Samih
  • 39
  • 6
0
<form id="my_form">
<!-- Your Form -->    
<a href="javascript:{}" onclick="document.getElementById('my_form').submit(); return false;">submit</a>
</form>

Before that, you have to set a <form>

Sinto
  • 3,915
  • 11
  • 36
  • 70