PDA

View Full Version : passing value to different page


red_rose
01-17-2007, 07:14 PM
hie..

i'm facing some problems in my php coding. the case is like this :

i have a page home.php which has a dropdown menu for the user to select an institution and a Go button. once the user selects an option, a new page named institution.php will be opened which will display information about the institution which was selected previously. i do not want to have different pages for each institution, so that's why i have one page to display the information according to the option selected in home.php. the problem is, when the user selects an option and click Go, the institution.php doesn't show any data from the db about the institution selected, not even the images that i have. can anyone tell me what's wrong?

home.php :



<?php

$inst = $_POST['inst'];

///////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM institution");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
///////////////////////////////////////////////////////////////////////////

?>

<html>
<head>
<title>:: Welcome ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>

<form name="form1" method="post" action='institution.php?id=<?php echo $row['Inst_Id']; ?>'>
<select name="inst">
<option value="" selected>Select Institution</option>
<?php do { ?>
<option value="<?php echo $row['Inst_Id']; ?>"><?php echo $row['InstName']; ?></option>
<?php } while ($row = mysql_fetch_array($result)); ?>
</select>
<input name="Go" type="submit" id="Go" value="Go">
</form>

</body>
</html>


institution.php :



<?php

if(isset($GET['id']))
{
$id = $GET['id'];

$query = sprintf("SELECT * FROM institution where Inst_Id = '$id'");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
}
?>

<html>
<head>
<title>:: Institution ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<table width="60%" height="386" border="2" cellpadding="2" cellspacing="2">
<tr>
<td><?php echo '<img src="images/'.$row['pic_name'].'">'; ?></td>
</tr>
<tr>
<td><?php echo $row['Inst_FullName']; ?></td>
</tr>
<tr>
<td><?php echo $row['Street'].$row['City'].$row['Postcode'].$row['State']; ?></td>
</tr>
<tr>
<td><?php echo $row['Description']; ?></td>
</tr>
</table>

<p>To make reservation :

<?php if(isset($_SESSION['userid'])) { ?>
<a href="reservation.php?id=<?php echo $row['Inst_Id']; ?>">Click here</a>
<?php } else { ?>
<a href="login.php">Click here</a>
<?php } ?>

</body>
</html>

davidj
01-17-2007, 07:55 PM
in your home.php you have 1 SQL statement. This statement populates your dropdowns so thats its job done

however you need another to query the database with your option choice

example...


<?php

$inst = $_POST['inst'];

//////// populates dropdown
///////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM institution");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
///////////////////////////////////////////////////////////////////////////

if($inst){
//////// gets record id to pass to next page
///////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM institution where Inst_Id= '$inst'");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

if($inst){

//////// do the redirection here
header("location:institution.php?id=$inst");
exit;

}

///////////////////////////////////////////////////////////////////////////
}


?>

<html>
<head>
<title>:: Welcome ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>

<form name="form1" method="post" action='home.php'>
<select name="inst">
<option value="" selected>Select Institution</option>
<?php do { ?>
<option value="<?php echo $row['Inst_Id']; ?>"><?php echo $row['InstName']; ?></option>
<?php } while ($row = mysql_fetch_array($result)); ?>
</select>
<input name="Go" type="submit" id="Go" value="Go">
</form>

</body>
</html>


oh and remember to point your form to the home.php page

red_rose
01-19-2007, 05:48 AM
hie david,

thanks for the correction, yeah i can redirect to institution.php but i still can't display the data from db for institution.php. i want to display the particular information based on the institution which the user selected in home.php but i failed to do so. can u check my institution.php coding whether i did rite? thanks alot

davidj
01-19-2007, 06:36 AM
can u check my institution.php coding whether i did rite? thanks alot

err you have to post it so i can see it

red_rose
01-19-2007, 06:51 AM
the coding is after home.php, in the first thread. anyway, i will post it again here


<?php

if(isset($GET['id']))
{
$id = $GET['id'];

$query = sprintf("SELECT * FROM institution where Inst_Id = '$id'");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
}
?>

<html>
<head>
<title>:: Institution ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<table width="60%" height="386" border="2" cellpadding="2" cellspacing="2">
<tr>
<td><?php echo '<img src="images/'.$row['pic_name'].'">'; ?></td>
</tr>
<tr>
<td><?php echo $row['Inst_FullName']; ?></td>
</tr>
<tr>
<td><?php echo $row['Street'].$row['City'].$row['Postcode'].$row['State']; ?></td>
</tr>
<tr>
<td><?php echo $row['Description']; ?></td>
</tr>
</table>

<p>To make reservation :

<?php if(isset($_SESSION['userid'])) { ?>
<a href="reservation.php?id=<?php echo $row['Inst_Id']; ?>">Click here</a>
<?php } else { ?>
<a href="login.php">Click here</a>
<?php } ?>

</body>
</html>

davidj
01-19-2007, 06:56 AM
you need to echo the SQL


echo $query = sprintf("SELECT * FROM institution where Inst_Id = '$id'");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);


this will return your statement and you will see if the $_GET is doing its job

red_rose
01-19-2007, 07:12 AM
i did as what you said, but i'm still getting errors. i attached the output to let you see it

red_rose
01-20-2007, 06:28 AM
hello there,

i have solved the problem. actually, the mistake was the $GET which is suppose to be $_GET.

so now i can view my results already :)

thanks alot for all your help, really appreciate it

davidj
01-20-2007, 10:46 AM
great

well done