Web Hosting Talk







View Full Version : How do I Pass a paramater to a 2nd screen?


AC1
09-05-2002, 08:13 AM
Hi

I have a screen with a form.

I read the records from my Mysql database and display the enteries in the form, which works fine, but I then want the action of the form to call a screen it_courses.php passing the value of subject_no which is related to the subject selected in the form.

I think the syntax of the action should be:
it_course.php?subject_no=the subject number selected

I have put this value in the option value, But I can't seem to get the selected entry to be passed, what ever I try. I think I am missing something obvious here, but I can't see what.

The record structure for subject is:
subject_no
Subject

The code I have at the moment is as follows, I have put **** where I think the problem lies:

Thanks in advance for any help,

Cheers
Andy

<blockquote>
<?

// Request the text of all the subjects

$result = @mysql_query("SELECT subject_no, subject FROM subject");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}

while ( $row = mysql_fetch_array($result) ) {
$subject=$row["subject"];
$subject_no=$row["subject_no"];
$option_block .="<option value=\"it_courses.php?subject_no=$subject_no\">$subject</option>";
}

?>
</blockquote>

<form method="post" action="it_courses.php?subject_no=****">
<p>Please select the required subject:<br>
<select name="subject_no">

<? echo "$option_block"; ?>

</select>

<p><input type="submit" value="Find Courses"></p>

</form>

Rich2k
09-05-2002, 09:42 AM
Your problem here is that you are using a GET method in the form action with the SAME variable name in the POST select field.

Therefore the variable in the querystring gets ignored.

i.e.

<form method="post" action="it_courses.php?subject_no=****">

<select name="subject_no">

jtrovato
09-05-2002, 12:27 PM
Andy,

I had the same problem when I started with forms. I didn't test this, but this should work:

</blockquote>
<?php

// Request the text of all the subjects

$result = @mysql_query("SELECT subject_no, subject FROM subject");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}

while ( $row = mysql_fetch_object($result) )
$option_block .="<option value=\"it_courses.php?subject_no=".$row->subject_no."\">".$row->subject."</option>";


?></blockquote>
<form method="post" action="it_courses.php">
<p>Please select the required subject:<br>
<select name="subject_no">
<? echo "$option_block"; ?>
</select>
<p>
<input type="submit" value="Find Courses">
</p>
</form>


Any questions let me know

John

Rich2k
09-05-2002, 02:35 PM
Remember to call the passed parameter as $_POST['subject_no'] rather than just $subject_no

jtrovato
09-05-2002, 05:51 PM
yeah my script doesn't work . sorry lol I could moditify it so it does work. unless you got it working already

John

AC1
09-06-2002, 07:16 AM
Hi John,

I have been trying to get it to work, but without any success at present.

If I do get it working I will post another message.

Cheers
Andy

benoire
09-06-2002, 07:52 AM
Instead of making the menu values 'it_courses.php?subject_no=xx', try making them just 'xx'. Then set your form's action to it_courses.php.

In it_courses.php, the subject_no.php value can then be called as $_POST['subject_no'].

Something like this:


<?

// Request the text of all the subjects

$result = @mysql_query("SELECT subject_no, subject FROM subject");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}

while ( $row = mysql_fetch_array($result) ) {
$subject=$row["subject"];
$subject_no=$row["subject_no"];
$option_block .="<option value=\"$subject_no\">$subject</option>";
}

?>

<form method="post" action="it_courses.php">
<p>Please select the required subject:<br>
<select name="subject_no">

<? echo "$option_block"; ?>

</select>

<p><input type="submit" value="Find Courses"></p>

</form>

Then in it_courses.php you'd have something like:

$subject_no = $_POST['subject_no'];

$result = @mysql_query("SELECT * FROM subject WHERE subject_no = $subject_no");

... and go on from there to pull the info from the database.

HTH :)

AC1
09-06-2002, 08:59 AM
Hi Benoire,

You have hit the nail on the head.

My understanding of how PHP works was incorrect.

The code I originally posted was working, the reason I thought it wasn't was because I was expecting to see the parameter on the end of the URL call. i.e. it_course.php?subject_no=the subject number selected

Where as I now realise that the data doesn't have to show up at the end of the URL to be passed.

Thanks to everyone for all their help,

Andy

benoire
09-06-2002, 09:06 AM
Data can sometimes show up on the end of the url in the form ?varname=value&varname2=value etc - this is called the GET method, as opposed to the POST method where information is submitted via a form and not displayed in the address bar. Each has their own use.

For example, if you had a list of news articles, you might generate a dynamic list of links, each one linking to "news.php?id=xxx" (this is the GET method) as you wouldn't want to have a form for each news article, and a drop down menu might not be convenient. However, if you had an order form and were submitting personal details, you wouldn't want to submit it using GET as you wouldn't want passwords etc being displayed in the address bar, and stored in the cache etc - so you would use a form with the POST method and do it this way.

I've probably not explained myself too well, but I hope its cleared things up a little at least! :)

AC1
09-06-2002, 09:52 AM
Hi benoire,

Your clarification is very good, that clears up a lot of the questions I still had outstanding in my mind about the handling of parameters between screens.

Thank you once again,

Andy

benoire
09-06-2002, 09:58 AM
Any time :)

pnet
09-10-2002, 03:59 PM
You guys make it so difficult for each other. To load a bunch of values from a database into a SELECT box simple use this:

<?php
echo "<select name=\"subject_no\">";
while($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[subject_no]\">$subject</option>";
}
echo "</select>";
?>

I honestly don't know why people go to such extend to make variables when they are not needed.

benoire
09-10-2002, 06:01 PM
Originally posted by pnet
You guys make it so difficult for each other. To load a bunch of values from a database into a SELECT box simple use this:

<?php
echo "<select name=\"subject_no\">";
while($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[subject_no]\">$subject</option>";
}
echo "</select>";
?>

I honestly don't know why people go to such extend to make variables when they are not needed.

Um... try testing your code before posting... what you posted will result in an error. You need:

echo "<option value=\"".$row[subject_no]."\">$subject</option>";

Had nobody else posted, that certainly would have made life more difficult for AC1 :rolleyes:

Yes, the idea you were posting might be a better way in terms of less code needed, but some people like to add extra steps in to either make things clearer for them, or for debugging purposes.

Why not use:

while($row = mysql_fetch_array(mysql_query("SELECT subject_no, subject FROM subject"))) {

if you're going to be picky? Not to mention $row['subject'] instead of $subject. And using ' ' instead of " " for less processing overhead.

Not meaning to sound scathing here, my point is that yes, while there are ways to improve on coding, there's no need to come in guns ablazing, making it sound like your way is the *obvious* way to do something and the other way is stupid...

Rich2k
09-10-2002, 06:02 PM
You can also use the $_REQUEST global array (although I would only advise it if you really have to).

For good programming you should either use $_POST['variable'] or $_GET['variable'] as you will almost certainly know what method you used but it is possible to use $_REQUEST['variable']