Click here to Skip to main content
15,914,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Im looking for some guidance. I want to list rows of data that is dependant on what was selected from the select menu. Ideally I'd like this to appear on the same page without a reload (AJAX?).

So far I have this:

HTML
<form action="animals.php" method="post">
            	<fieldset>
                    <select>
                        <option value="species">Species</option>
                        <option value="enclosure">Enclosure</option>
                        <option value="animal">Animal</option>
                    </select>
                    <input type="submit"  />
                </fieldset>
            </form>


And the PHP code:

PHP
<?php
include 'dbconnect.php';
if(isset($_POST['value'])) { 
    if($_POST['value'] == 'species') {  
        $query = "SELECT Speciesname FROM SPECIES inner join ANIMAL on ANIMAL.Species=SPECIES.Speciesid";   
    }   
    elseif($_POST['value'] == 'enclosure') {      
        $query = "SELECT Enclosurename FROM ENCLOSURE inner join ANIMAL on ANIMAL.Enclosure=ENCLOSURE.Enclosureid";   
    } else {   
        $query = "SELECT Animalname FROM ANIMAL";   
    }   
    $sql = mysql_query($query);   
    while ($row = mysql_fetch_array($query)){  
        $species = $row["Speciesname"];
		$enclosure = $row["Enclosurename"];
		$animal = $row["Animalname"];  
    }  
} 
?>


I need to echo 1 set of results but I am unaware of how to do this depending on what was submitted from the form. E.g. If the user selected species, all species will be shown from the database, and nothing else.

Thanks.
Posted

1 solution

Take a look at my article at the following link for the ajax part:
AJAX vs. Non-AJAX Calls All-in-One Page

But for the sql part, should use php prepared statement to prevent sql injection like this example:
prepared-statements-in-php-and-mysqli
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900