hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : How do I Pass a paramater to a 2nd screen?
Reply

Forum Jump

How do I Pass a paramater to a 2nd screen?

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 09-05-2002, 08:13 AM
AC1 AC1 is offline
Newbie
 
Join Date: Aug 2002
Location: Originally from Bolton, England. I have been working in Brussels, Belgium for the last 2.5 years.
Posts: 29
Question

How do I Pass a paramater to a 2nd screen?


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>



Sponsored Links
  #2  
Old 09-05-2002, 09:42 AM
Rich2k Rich2k is offline
Web Hosting Master
 
Join Date: May 2002
Location: UK
Posts: 2,994
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">

  #3  
Old 09-05-2002, 12:27 PM
jtrovato jtrovato is offline
Aspiring Evangelist
 
Join Date: Aug 2002
Location: Long Island
Posts: 427
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

__________________
John Trovato
In Office Networks, LLC
Programmer, Cisco Network Engineer, Roofer, Biochemist, and Conductor.

Sponsored Links
  #4  
Old 09-05-2002, 02:35 PM
Rich2k Rich2k is offline
Web Hosting Master
 
Join Date: May 2002
Location: UK
Posts: 2,994
Remember to call the passed parameter as $_POST['subject_no'] rather than just $subject_no

  #5  
Old 09-05-2002, 05:51 PM
jtrovato jtrovato is offline
Aspiring Evangelist
 
Join Date: Aug 2002
Location: Long Island
Posts: 427
yeah my script doesn't work . sorry lol I could moditify it so it does work. unless you got it working already

John

__________________
John Trovato
In Office Networks, LLC
Programmer, Cisco Network Engineer, Roofer, Biochemist, and Conductor.

  #6  
Old 09-06-2002, 07:16 AM
AC1 AC1 is offline
Newbie
 
Join Date: Aug 2002
Location: Originally from Bolton, England. I have been working in Brussels, Belgium for the last 2.5 years.
Posts: 29
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

  #7  
Old 09-06-2002, 07:52 AM
benoire benoire is offline
Registered User
 
Join Date: Jul 2001
Location: Coventry, England
Posts: 130
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:

PHP Code:
<? 

// 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:
PHP Code:
$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

  #8  
Old 09-06-2002, 08:59 AM
AC1 AC1 is offline
Newbie
 
Join Date: Aug 2002
Location: Originally from Bolton, England. I have been working in Brussels, Belgium for the last 2.5 years.
Posts: 29
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

  #9  
Old 09-06-2002, 09:06 AM
benoire benoire is offline
Registered User
 
Join Date: Jul 2001
Location: Coventry, England
Posts: 130
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!

  #10  
Old 09-06-2002, 09:52 AM
AC1 AC1 is offline
Newbie
 
Join Date: Aug 2002
Location: Originally from Bolton, England. I have been working in Brussels, Belgium for the last 2.5 years.
Posts: 29
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

  #11  
Old 09-06-2002, 09:58 AM
benoire benoire is offline
Registered User
 
Join Date: Jul 2001
Location: Coventry, England
Posts: 130
Any time

  #12  
Old 09-10-2002, 03:59 PM
pnet pnet is offline
Newbie
 
Join Date: Sep 2002
Posts: 8
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.

  #13  
Old 09-10-2002, 06:01 PM
benoire benoire is offline
Registered User
 
Join Date: Jul 2001
Location: Coventry, England
Posts: 130
Quote:
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

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...

  #14  
Old 09-10-2002, 06:02 PM
Rich2k Rich2k is offline
Web Hosting Master
 
Join Date: May 2002
Location: UK
Posts: 2,994
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']

Reply

Related posts from TheWhir.com
Title Type Date Posted
Announcing the WHD.global VIP Pass Winner! Blog 2014-03-27 16:00:39
SoftLayer - The Water Fountains are Overflowing Blog 2013-06-07 11:50:50
The DIY Website Revolution Will be Televised Web Hosting News 2013-05-17 12:34:27
Amazon, VeriSign Among Winners in ICANN New gTLD Initial Evaluation Web Hosting News 2013-03-25 10:30:07
SherWeb Partners Can Pass Off Customer Support with New Distributor Program Web Hosting News 2012-07-12 13:49:23


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?