-2

I have a search function(car_search.php) which allow users to select a type of car,year and colour and be directed to the page of the searched car. How do i direct the user to Toyota Highlander 2012 page in case he selects that from the search page?

car_search.php

            <form  method="POST" action="display_search.php">
                <div class="search_lft">
                    <div class="form-group">
                    <label>Type of Vehicle</label>
                    <select name="car_type" size="1" style="width:280px" onchange="javascript:selVal_(this.value)" >
                        <option selected="selected" value="min" >Vehicle Type</option>
                        <option value="Toyota Highlander" title="Toyota Highlander">Toyota Highlander</option>
                        <option value="Toyota Rav4" title="Toyota Rav4">Toyota Rav4</option>
                        <option value="Toyota Corolla" title="Toyota Corolla">Toyota Corolla</option>
                        <option value="Nissan Altima" title="Nissan Altima">Nissan Altima</option>
                        <option value="Honda Civic" title="Honda Civic">Honda Civic</option>
                        <option value="Honda Civic-EX" title="Honda Civic-EX">Honda Civic-EX</option>
                    </select>
                    </div>          
                </div>
                <div class="search_mid">            
                <div class="model">
                        <label>Year Made</label>
                        <select name="car_year" id="makeid" size="1" style="width:280px;" onchange="javascript:selVal_(this.value)">
                                <option value="min" selected="selected">Car Year</option><option value="2011" title="2011">2011</option><option value="2012" title="2012">2012</option><option value="2013" title="2013">2013</option><option value="2014" title="2014">2014</option><option value="2015" title="2015">2015</option></select>

                </div>

                </div>

                <div class="search_right">
                    <div class="location">
                        <label>Exterior Colour</label><select name="exterior_colour" size="1" style="width:280px" class="">
                            <option value="" selected="selected">Any Colour</option>
                            <option value="White">Pearl White</option>
                            <option value="Silver">Metallic Red</option>
                            <option value="Green">Metallic Grey</option>
                            <option value="Dark Green">Metallic Blue</option>
                            <option value="Blue">Red</option>
                            <option value="Dark Blue">Black</option>
                            <option value="Dark Blue">Grey</option>
                            <option value="Dark Blue">White</option>
                            <option value="Dark Blue">Wine</option>
                            <option value="Dark Blue">BlueBlack</option>
                            <option value="Other">Other</option>
                        </select>
                    </div>  


                </div><br>

            <div style="margin-right:900px; margin-top:110px; height:10px; width:180px;"></div>
            <div style="display: block;" name="searchbox" id="searchbox" class="searchbox">
                    <p>
                        <button style="width:120px;" name="searchtext" type="submit" class="btn btn-info">
                            <span class="glyphicon glyphicon-search"></span> Search
                        </button>
                    </p>
            </div>

            </div>
            </form>

display_search.php

<?php
if(isset($_POST['searchtext'])){

    $car_type = $_POST['car_type'];
    $car_year = $_POST['car_year'];


if($car_type == 'Toyota Highlander' && $car_year == '2012') 
{
    header('Location: toyota_highlander_2012.php');
}

else{ echo  "No results";
}
}
?>
Emmanuel Gamor
  • 69
  • 1
  • 10
  • Your current code is not working? –  Aug 12 '15 at 16:07
  • 1
    @caCtus Of course not. He assigns the $_POST value of car_type to `$car_type` and uses `$car_tape` in the if clausel. I would suggest activating php errors. – Charlotte Dunois Aug 12 '15 at 16:09
  • 1
    I think the concept of dynamic web pages seems to have competely flown over your head. – RiggsFolly Aug 12 '15 at 16:11
  • Yea that is what I am using and its not working It just direct me to a blank page. @caCtus – Emmanuel Gamor Aug 12 '15 at 16:37
  • As @CharlotteDunois suggested, [activate PHP errors](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). –  Aug 12 '15 at 16:38

1 Answers1

0

You going in the right way, but mistyped your $type_car for $tape_car in your if clausule.

Also as a suggestion your should create some collections, one for cars, one for years, one for colors instead of trying to compare by their name set as an option for a select argument, this is not reliable at all.

As you seem to be writing procedural programming, I suggest you keeping this form and logic in same page for easy maintenance you can choose a name like car_module.php and try this code

<?php
    #this code still needs work and was not testing, but i hope it works
    $years  = array(
                "2011",
                "2012",
                "2013",
                "2014",
                "2015");

    $cars   = array(
                "01" => "Toyota Highlander",
                "02" => "Toyota Rav4",
                "03" => "Toyota Corolla",
                "04" => "Nissan Altima",
                "05" => "Honda Civic",
                "06" => "Honda Civic-EX");

    $colors = array(
                "White" => "Pearl White",
                "Silver" => "Metallic Red",
                "Green" => "Metallic Grey",
                "Dark Green" => "Metallic Blue",
                "Blue" =>"Red",
                "Dark Blue" => "Black",
                "Dark Blue" => "Grey",
                "Dark Blue" => "White",
                "Dark Blue" => "Wine",
                "Dark Blue" => "BlueBlack",
                "Other" => "Other");


    if(isset($_POST['searchtext'])){

        $carType = $_POST['car_type'];
        $carYear = $_POST['car_year'];

        if ( (int)$carType == 01 && (int)$carYear == 2012 ) {

            header('Location: toyota_highlander_2012.php');

        } else { 

            echo  "No results"; 

        }

    }


?>
<form  method="POST" action="car_module.php">
    <div class="search_lft">
        <div class="form-group">
        <label>Type of Vehicle</label>
        <select name="car_type" size="1" style="width:280px" onchange="javascript:selVal_(this.value)" >

            <option selected="selected" value="min" >Vehicle Type</option>

            <?php  foreach($cars as $carKey => $carName) { ?>
                <option value="<?php echo $carKey ?>" title="Toyota Rav4"><?php echo $carName; ?></option>
            <?php } ?>

        </select>
        </div>          
    </div>

    <div class="search_mid">            
        <div class="model">
                <label>Year Made</label>
                <select name="car_year" id="makeid" size="1" style="width:280px;" onchange="javascript:selVal_(this.value)">

                    <option selected="selected" value="0" >Year</option>

                    <?php  foreach($years as $year) { ?>
                        <option value="<?php echo $year ?>" ><?php echo $year; ?></option>
                    <?php } ?> 

                </select>
        </div>
    </div>

    <div class="search_right">
        <div class="location">
            <label>Exterior Colour</label><select name="exterior_colour" size="1" style="width:280px" class="">
                <?php  foreach($colors as $color) { ?>
                    <option value="<?php echo $color ?>" ><?php echo $color; ?></option>
                <?php } ?> 
            </select>
        </div>  


    </div><br>

<div style="margin-right:900px; margin-top:110px; height:10px; width:180px;"></div>
<div style="display: block;" name="searchbox" id="searchbox" class="searchbox">
        <p>
            <button style="width:120px;" name="searchtext" type="submit" class="btn btn-info">
                <span class="glyphicon glyphicon-search"></span> Search
            </button>
        </p>
</div>

</div>
</form> 
AleMelo
  • 120
  • 4