Results 1 to 14 of 14
  1. #1
    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>

  2. #2
    Join Date
    May 2002
    Location
    UK
    Posts
    2,997
    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. #3
    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.

  4. #4
    Join Date
    May 2002
    Location
    UK
    Posts
    2,997
    Remember to call the passed parameter as $_POST['subject_no'] rather than just $subject_no

  5. #5
    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. #6
    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. #7
    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. #8
    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. #9
    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. #10
    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. #11
    Join Date
    Jul 2001
    Location
    Coventry, England
    Posts
    130
    Any time

  12. #12
    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. #13
    Join Date
    Jul 2001
    Location
    Coventry, England
    Posts
    130
    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. #14
    Join Date
    May 2002
    Location
    UK
    Posts
    2,997
    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']

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •