2

Apologies if this is an amateur question, my PHP skills are still in development. I've currently created a search box with the current code.

<?php    
        // Tyre search setup
        $profileSql = "SELECT DISTINCT `profile` FROM `tyres` WHERE `profile` > 0 ORDER BY `profile`";
        $profileResult = $db->query($profileSql);

        $widthSql = "SELECT DISTINCT `width` FROM `tyres` WHERE `width` > 0 ORDER BY `width`";
        $widthResult = $db->query($widthSql);

        $diamSql = "SELECT DISTINCT `diam` FROM `tyres` WHERE `diam` > 0 ORDER BY `diam`";
        $diamResult = $db->query($diamSql);

        if(isset($_GET['profile']) && isset($_GET['width']) && isset($_GET['diam'])) {
            $profile = $_GET['profile'];
            $width = $_GET['width'];
            $diam = $_GET['diam'];

            $tyreSql = "SELECT * FROM `tyres` WHERE `profile` = " . $profile . " AND `width` = " . $width . " AND  `diam` = " . $diam . " AND rrp > 0 ORDER BY `profile`";
            $tyreResult = $db->query($tyreSql);
        }   
?> 

Here is the form this logic is tied to.

<form id="formTyreSearch" name="tyreSearch" method="GET">
    <h2>Tyre Search</h2>
    <p>Use our search below to find the tyre for your car.</p>
        <div class="form-group">Tyre Profile:
            <select name = "diamSearch">
                <?php while($row=mysqli_fetch_assoc($profileResult)) : ?>
                    <option value="<?php echo $row['profile']; ?>"><?php echo $row['profile']; ?></option>
                <?php endwhile; ?>
            </select>
            </div>

       <div class="form-group">Tyre Width:
           <select name = "pcdSearch">
               <?php while($row=mysqli_fetch_assoc($widthResult)) : ?>
                    <option value="<?php echo $row['width']; ?>"><?php echo $row['width']; ?></option>
               <?php endwhile; ?>
            </select>
       </div>

       <div class="form-group">Tyre Diam:
           <select name = "pcdSearch">
               <?php while($row=mysqli_fetch_assoc($diamResult)) : ?>
                    <option value="<?php echo $row['diam']; ?>"><?php echo $row['diam']; ?></option>
               <?php endwhile; ?>
          </select>
      </div>

        <button type="submit" name="filterOptions" value="displayManu" class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Search Now</button>
</form>

Now from here I assume the values selected in the form are set in the $tyreResult variable.

I want to pass this result to a tyres.php page and display the search result there, what is best practice to do this? and how should I handle if the user hasn't selected all three values, as there is going to be other content on the site I don't really want to reload the page and display an error at the top...

Jcode
  • 195
  • 2
  • 12
  • I suggest converting your result to JSON and use POST to send the data to the other page. You don't need a form. Just get the JSON and convert again in your result object. Check this http://php.net/manual/en/book.json.php to learn how to manage JSON data in PHP. Then check this http://php.net/manual/en/book.curl.php for curl to POST the JSON data to the other PHP page. – mhyst May 12 '16 at 23:58

1 Answers1

1

Just add an action attribute to your form and move the result logic into tyres.php. If they haven't selected all values, redirect them back to the previous page. But that shouldn't happen unless the user mischievously alters your HTML. By default, the first options of your <select> tags will be selected.

search.php

<?php    
// Tyre search setup
$profileSql = "SELECT DISTINCT `profile` FROM `tyres` WHERE `profile` > 0 ORDER BY `profile`";
$profileResult = $db->query($profileSql);

$widthSql = "SELECT DISTINCT `width` FROM `tyres` WHERE `width` > 0 ORDER BY `width`";
$widthResult = $db->query($widthSql);

$diamSql = "SELECT DISTINCT `diam` FROM `tyres` WHERE `diam` > 0 ORDER BY `diam`";
$diamResult = $db->query($diamSql);
?>

<form id="formTyreSearch" method="get" action="tyres.php">
    <h2>Tyre Search</h2>
    <p>Use our search below to find the tyre for your car.</p>
    <div class="form-group">
        Tyre Profile:
        <select name="profile">
            <?php while ($row = mysqli_fetch_assoc($profileResult)) : ?>
                <option value="<?php echo $row['profile'] ?>"><?php echo $row['profile'] ?></option>
            <?php endwhile ?>
        </select>
    </div>
    <div class="form-group">
        Tyre Width:
        <select name="width">
        <?php while ($row = mysqli_fetch_assoc($widthResult)) : ?>
            <option value="<?php echo $row['width'] ?>"><?php echo $row['width'] ?></option>
        <?php endwhile ?>
        </select>
    </div>
    <div class="form-group">
        Tyre Diam:
        <select name="diam">
        <?php while ($row = mysqli_fetch_assoc($diamResult)) : ?>
            <option value="<?php echo $row['diam'] ?>"><?php echo $row['diam'] ?></option>
        <?php endwhile ?>
        </select>
    </div>
    <button type="submit" name="filterOptions" value="displayManu" class="btn btn-default btn-sm btn-primary">
      <i class="fa fa-pencil"></i> Search Now
    </button>
</form>

tyres.php

<?php
if(isset($_GET['profile'], $_GET['width'], $_GET['diam'])) {
    $profile = $_GET['profile'];
    $width = $_GET['width'];
    $diam = $_GET['diam'];

    $tyreSql = "SELECT * FROM `tyres` WHERE `profile` = " . $profile . " AND `width` = " . $width . " AND  `diam` = " . $diam . " AND rrp > 0 ORDER BY `profile`";
    $tyreResult = $db->query($tyreSql);
} else {
    header('Location: search.php');
    exit;
}
?>

<!-- show the results -->
Mikey
  • 6,728
  • 4
  • 22
  • 45
  • Hey Mikey, doesn't seem to pass through the values, just has tyres.php?tyreSearch= in the header. Also cant use the header('Location: ') as I get this error Warning: Cannot modify header information - headers already sent by (output started at includes\quickModalTyres.inc.php:127) in \tyres.php on line 55, 55 being the header, line 127 being – Jcode May 13 '16 at 01:04
  • That's weird. I would expect `tyres.php?pcdSearch=` based off your original code. You need to give unique names to your ` – Mikey May 13 '16 at 02:30
  • Hey Mikey, Thanks I had another look and changed it, I may end up editing it at a later date to use cases, so any one of the parameters can be used independently, but that's for another day. I'll have a look into that post, the modal has been giving me issues for awhile now (its where that ob_get_clean is called. – Jcode May 13 '16 at 02:45