Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP: Combo box FdSc Module 109 Server side scripting and

Similar presentations


Presentation on theme: "PHP: Combo box FdSc Module 109 Server side scripting and"— Presentation transcript:

1 PHP: Combo box FdSc Module 109 Server side scripting and
Database design 2011

2 Example We will create a form that has a drop down box populated by an SQL query When the submit button is pressed the value selected will be displayed The value will then be used to select specific data from a table

3 PHP isset isset Determines if a variable is set and is not null
Can be used to see if form has been submitted or not We will use it to see if the user has submitted a selection from the combo box If they have we use the value to select more data If they haven’t we display the combo box

4 Structure if (isset($_POST['firstname'])) { select more data } else { display form }

5 Connection <?php // Connect to the database mysql_connect(“localhost", "studentID", “password") or die(mysql_error()); mysql_select_db("studentID_library") or die(mysql_error());

6 Check submission // Has the form been submitted? // If it has, display the selection if (isset($_POST['firstname'])) { echo "Firstname selected was: "; echo $_POST['firstname']; echo "<br/>";

7 Display new data $result = mysql_query("SELECT * FROM borrower WHERE firstname = '".$_POST['firstname']."'"); while ($row = mysql_fetch_array($result)) { echo "<br/>"; echo $row[2]; echo" "; echo $row["lastname"]; }

8 Create the form if not done yet
}else { // Create the form, post to the same file echo "<form method='post‘ action='combo1.php'>"; // Form a query to populate the combo-box $queryitem = "SELECT DISTINCT firstname FROM borrower;";

9 Build the combo box // Successful query?
if($result = mysql_query($queryitem)) { // If there are results returned, prepare combo-box if($success = mysql_num_rows($result) > 0) { // Start combo-box echo "<select name='firstname'>"; echo "<option>-- Select Item -- </option>";

10 Complete the combo box // For each item in the results...
while ($row = mysql_fetch_array($result)) // Add a new option to the combo-box echo "<option value='$row[firstname]'>$row[firstname] </option>"; // End the combo-box echo "</select>"; }

11 Tidy up and add a button // No results found in the database
else { echo "No results found."; } } // Error in the database else { echo "Failed to connect to database."; } // Add a submit button to the form echo "<input type='submit' value='Submit' /></form>"; ?>

12 Result


Download ppt "PHP: Combo box FdSc Module 109 Server side scripting and"

Similar presentations


Ads by Google