Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting information from a form 2

Status
Not open for further replies.

mrobinson

MIS
Oct 11, 2002
36
GB
i am having trouble getting information from one form to another form. I am trying to use a drop down menu with values from a database and once one is selected the value is used in an argument on the next from. I have tried printing the value to the screen in the next form but it comes up blank. I have other forms that exchange data so i think the problem is with my loop. The text fro which is below:

$display_block = "<h1>Select Course</h1>";

//get parts of records
$get_list = "select ctitle as display_name from course";
$get_list_res = mysql_query($get_list) or die(mysql_error());



if (mysql_num_rows($get_list_res) < 1) {
//no records
$display_block .= "<p><em>Sorry, no records to select!</em></p>";


} else {

$display_block .= "
<form method=\"post\" action=\"insert_attend2.php\">
<P><strong>Select a Course:</strong><br>
<select name=\"sel_id\">
<option value=\"\">-- Select One --</option>";

while ($recs = mysql_fetch_array($get_list_res)) {
$id = $recs['ctitle'];
$display_name = stripslashes($recs['display_name']);

$display_block .= "<option value=\"$id\">
$display_name</option>";
}

$display_block .= "
</select>
<p><input type=\"submit\" name=\"submit\" value=\"Select\"></p>
</FORM>";
}

Could anyon please tell me how i would call for the value selected in the next form as i have tried everything i can think of.

Many thanks


 
When the form is submitted to insert_addent2.php, the selected value from the select tag "sel_id" will be available in $_POST['sel_id'].

Is that what you meant?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I have tried this and it gives me a parse error on the line i try to call it as an argument. unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING.

The code for the page is:


<?php
//connect to database
$conn = mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("attendance",$conn) or die(mysql_error());


$display_block = "<h1>Select Module</h1>";

//get parts of records

$get_list3 = "select mcode as display_name from module where ctitle =$_POST['sel_id']";
$get_list_res3 = mysql_query($get_list3) or die(mysql_error());



if (mysql_num_rows($get_list_res3) < 1) {
//no records
$display_block .= "<p><em>Sorry, no records to select!</em></p>";



} else {
//has records, so get results and print in a form
$display_block .= "
<form method=\"post\" action=\"insert_attend3.php\">
<P><strong>Select a module:</strong><br>
<select name=\"sel_mod\">
<option value=\"\">-- Select One --</option>";

while ($recs = mysql_fetch_array($get_list_res2)) {
$id = $recs['mcode'];
$display_name = stripslashes($recs['display_name']);

$display_block .= "<option value=\"$id\">
$display_name</option>";
}

$display_block .= "
</select>
<input type = \"hidden\" name=\"mod\" value=\"$_POST[sel_mod]\">
<p><input type=\"submit\" name=\"submit\" value=\"Select\"></p>
</FORM>";
}


?>
<html>
<head>
<title>Select Module Group</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>

Do you have any suggestions please? There is deffinatly data in the database.

Thanks











 
That parse error is because PHP doesn't like associative array references inside quoted strings. Use an explicit string concatenation instead:

Code:
 $get_list3 = "select mcode as display_name from module where ctitle =" .$_POST['sel_id'];

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
i don't totally understand what you mean but i put the code in you gave me and played about with it but it keeps giving me a mysql error. Any other ideas.

Thanks very much for the help, it is much appriciated
 
you have an error in your SQL syntax. check the manual that corresponds to your MYSQL server version for the right syntax to use near" at line 1.

the query works if i substitute the $_POST['sel_id'] for a value. I have tried printing the $_POST['sel_id'] to the screen but this just comes up blank so i don't think the value is coming across. My mysql version is 4.0.15 if that helps

thanks
 
mrobinson:
Your version of PHP does help. $_POST only exists in the language as of PHP version 4.1.0.

Change your reference to $_POST['sel_id'] to $HTTP_POST_VARS['sel_id'];





On the subject of associative array references inside strings:

$_POST is an associative array. The indeces of the elements of the array are strings, not numbers. PHP doesn't like references to associative arrays inside strings.

Given the following code:
Code:
<?
$a = 'foo';
$b = array('bar', 'baz');
$c = array('bar' => 1, 'baz' => 2);

$string1 = "test $a";
$string2 = "test $b[0]";
$string3 = "test $c['bar']";
?>

$a is a single variable, $b is a numeric array, and $c is an associative array.

PHP will accept the lines which assign values to $string1 and $string2. But it will not accept the line which assigns a value to $string3, because the reference to the associative array $c appears inside the quotes. This is just a quirk of the interpreter.

But what is happening in the lines that assign values to $string1, $string2 and $string3 is actually string concatenation. In the case of the assignment to $string1, the assignment statement concatenates "test " and the value in $a. This is implicit concatenation.

Another way to perform the concatenation is through use of PHP's "." concatenation operator.

For example, the lines

$string1 = "test $a";
and
$string1 = "test " . $a;

are functionally equivalent. It's just that the first example line uses implicit concatenation and the second explicit concatenation.

Since PHP won't let you use implicit concatenation, you must use explicit concatenation. Referring back to the code in the code block, you can't do:

$string3 = "test $c['foo']";

but you can do

$string3 = "test " . $c['foo'];




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
note the following:

Code:
<select name=\"sel_mod\">
         <option value=\"\">-- Select One --</option>";

but your sql query is:
Code:
$get_list3 = "select mcode as display_name from module where ctitle =$_POST['sel_id']";

select name="sel_mod" *different than* $_POST['sel_id']

could it be your problem?
 
sorry, my mysql version is 4.0.15, my version of php is 4.3.3. I tried the above but still no joy. Thankyou very much for explaining about arrays. It is starting to make a lot more sense now.
I have an example which does a similar thing and have literally copied that just changing the bits i need to and while the example works i just can't get this to work no matter what i change.
 
i am trying to call the variable from the previous form into the current form. as far as i can see the variables are correct?
 
Add the following lines to the beginning of insert_attend2.php:

print_r ($_POST);
exit();

Then point your browser to the first page we've discussed, choose a value and submit.

What is output to the browser?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I'm sorry, I didn't see it...

Could you post the result (http page) of the form as the browser see of: (this is your first script and the source for sel_id var)

Code:
 <form method=\"post\" action=\"insert_attend2.php\">
         <P><strong>Select a Course:</strong><br>
         <select name=\"sel_id\">
         <option value=\"\">-- Select One --</option>";

         while ($recs = mysql_fetch_array($get_list_res)) {
             $id = $recs['ctitle'];
             $display_name = stripslashes($recs['display_name']);

             $display_block .= "<option value=\"$id\">
                  $display_name</option>";
         }

        $display_block .= "
        </select>
        <p><input type=\"submit\" name=\"submit\"  value=\"Select\"></p>
        </FORM>";

Cheers.
 
Im sorry, i can't do this as i am working on a stand alone machine whilst doing work with PHP and this machine doesn't have it installed. The full code for the page is as follows:


<?php
//connect to database
$conn = mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("attendance",$conn) or die(mysql_error());



$display_block = "<h1>Select Course</h1>";

//get parts of records
$get_list = "select ctitle as display_name from course";
$get_list_res = mysql_query($get_list) or die(mysql_error());



if (mysql_num_rows($get_list_res) < 1) {
//no records
$display_block .= "<p><em>Sorry, no records to select!</em></p>";


} else {

$display_block .= "
<form method=\"post\" action=\"insert_attend2.php\">
<P><strong>Select a Course:</strong><br>
<select name=\"sel_id\">
<option value=\"\">-- Select One --</option>";

while ($recs = mysql_fetch_array($get_list_res)) {
$id = $recs['ctitle'];
$display_name = stripslashes($recs['display_name']);

$display_block .= "<option value=\"$id\">
$display_name</option>";
}

$display_block .= "
</select>
<p><input type=\"submit\" name=\"submit\" value=\"Select\"></p>
</FORM>";
}






?>
<html>
<head>
<title>Select Course</title>
</head>
<body>
<?php echo $display_block;?>
</body>
</html>



Thanks
 
I'm quite sure that the <select> has option, but values are "" (null).

take a look to the code:
Code:
    //get parts of records
     $get_list = "select ctitle as display_name  from course";
     $get_list_res = mysql_query($get_list) or die(mysql_error());

SELECT ctitle AS display_name FROM course

you select only one column with name "display_name", now you fill the option tags with:
Code:
while ($recs = mysql_fetch_array($get_list_res)) {
             $id = $recs['ctitle'];
             $display_name = stripslashes($recs['display_name']);

             $display_block .= "<option value=\"$id\">
                  $display_name</option>";
         }

using:
$id = $recs['ctitle'];
$recs['display_name']


your select query has "ctitle AS display_name" not "ctitle,display_name".. you are geting only *one* column named "display_name".
 
yes, i selected a value then submitted it and it opened the next page to which it keeps giving me an error or gives the 'sorry no records to display' message. If i write the value of what i have selected where the $_POST['sel_id'] is in the second form it selects the values into the drop down menu as i wanted.
 
I insist, your select query has "ctitle AS display_name" not "ctitle,display_name".. you are geting only *one* column named "display_name" and thus geting null values for the options.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top