Page 1 of 1

Outputting several elements from a database with a PHP form

Posted: Wed Aug 26, 2009 9:55 am
by Fabrizzio PHP
Hi,

I’m a PHP beginner learning through online tutorials and need some help:
On my index.php file I’ve got a dynamic drop down list which displays the “university” names and when the form is submitted, it should output from my databse: the “content”, “post_code” and “borough” in my result.php. Unfortunately, all I’ve managed to display is the name of the university which is assigned to the "select value". Can I please have assistance on how to output all the database info based on the “select” university?

I’ve got a database called "map" with the following table:

id
university
position
visible
content
post_code
borough

My index.php file has the following code:

Code: Select all

<?php
// Connect database
mysql_connect("localhost","root","xxx");
mysql_select_db("map"); 
 
// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.
if(isset($select)&& $select!=""){
$select=$_GET['select'];
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 </head>
 
<body>
<form id="form1" name="form1" method="post" action="result.php""> 
Search by borough :
<select name="select">
<option value="">--- Select ---</option>
<?php
// Get records from database (table "subjects").
$list=mysql_query("SELECT * FROM subjects ORDER BY id ASC");
 
// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<?php echo $row_list['university']; ?>" <?php if($row_list['id'] == '$select'){ echo "selected"; } ?>><?php echo $row_list['university']; ?></option>
<?php
// End while loop.
}
?>
</select> 
<input type="submit" name="Submit" value="select" />
</form>
 
<?php
mysql_close();
?>
</p>
</body>
</html>
 
//My result.php contains the following:

Code: Select all

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
 
<html>
<head>
</head>
<body>
    The University is: <?php echo $_POST["select"]; ?>
     The Postcode is:  
     The content (university description is):
</body>
</html>
 

Re: Outputting several elements from a database with a PHP form

Posted: Wed Aug 26, 2009 1:35 pm
by yacahuma
you are only sending select. What else do you want to do? You do not have any other elements in your form

Re: Outputting several elements from a database with a PHP form

Posted: Wed Aug 26, 2009 2:15 pm
by Fabrizzio PHP
Hi yacahuma,

You are right, the form is a php drop down menu which only provides you all the available Universities and once you have selected one of them and submit, the result.php file should echo the "select one" through this code:

Code: Select all

 
The University is: <?php echo $_POST["select"]; ?>
 
My problem I would also like to display the postcode, borough, and content of the selected (this is what I don't know how to do it)

ie. let's say you choose UCL University in the drom down menu, and when you submit it, the result.php should echo the selected name, and as well output the other info in the database related to this selection.

I'm still learning PHP so maybe you are right and I'm missing coding or explaining myself better. :banghead: Please let me know if this is clearer now.
Thanks,
Fabrizzio