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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables from html form to php page - variable not passed via $_POST

Status
Not open for further replies.

phpPupil

Programmer
Mar 9, 2013
2
0
0
US
Having an issue that I just spend 8 hours on. I am trying to pass variables to my php page please look at the following code snippet.

Code:
while ($row = mysql_fetch_array($result)) {

$id = $row['id'];
$f_name = $row['f_name'];
$l_name = $row['l_name'];

$option_block .= "<option value=\"$id\">$l_name, $f_name</option>";

the id value is passed but I can't access $l_name or $f_name variables on next page

Code:
$sql = "SELECT
f_name, l_name, address1, address2, address3, postcode, country, prim_tel, sec_tel, email, birthday
FROM $table_name
WHERE id = \"$_POST[id]\"
";

$result = @mysql_query($sql, $connection) or die ("Couldn't execute query.");

while ($row = mysql_fetch_array($result)) {

$_POST["f_name"] = $row['f_name'];
$l_name = $row['l_name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
$address3 = $row['address3'];
$postcode = $row['postcode'];
$country = $row['country'];
$prim_tel = $row['[prim_tel'];
$sec_tel = $row['sec_tel'];
$email = $row['email'];
$birthday = $row['birthday'];

}

?>

Even using an array I can't find variables $f_name and $l_name I've been playing around trying to figure out how to do this. But my php info shows the id variable and value but no l_name or f_name

Any suggestions on how to do this? the value is passed when an option is select from drop down.
 
the while clause is redundant as there will only ever be one result if the ID is unique. so take it out.

the f_name and l_name are in the $row['f_name'] etc array elements.
you have an error in the prim_tel key where you have duplicated the opening square bracket.

I can see no reason to assign the value of $row['f_name'] to a $_POST element. that is bad bad practice.

and just in case you were wondering, assigning something to a $_POST variable does not make it available to another 'page'. each 'page' (as you are referring to) is a wholly separate thread or instance of php. there is no implicit sharing between them. if you want to share then you have to 'fake' a state-ful connection through the use of session elements or by passing variables in a query string or hidden html form. There is (little) no alternative.

if you are referring to 'pages' as php files then the answer is different. you can call any php file you like, and the functions within that file, by using the php function include() or require() or require_once() or include_once(), depending on your requirements. The functions and variables in the included files will inherit the scope of the calling function. so if called from the global scope, the functions and variables will be in the global scope etc. If you are uncertain about what 'scope' means, read the manual on the subject.
 
Thanks Jpadie for the help, The real problem is that f_name and l_name have no value. The original page is pick.php this is where the user selects and option that POSTS the primary key (id) to the next php page show.php. On this page I need to access the f_name and l_name that is associated with the ID posted to the page.

For instance there are users in my DB with different attributes
Code:
ID    first name    last Name     address

25     Michael      Peters         123 anywhere street
32     Bob          Sliter         456 elmo driv
now there is a option select on pick.php (drop down) to select either Michael or Bob. If Michael is selected the ID value (25) is posted to the show.php page. Now on that page I want to display a message welcome Michael Peters. But I can't because I don't have the first name and last name value I only have the id (25). I need to access first name and last name on show.php. How can I do this?

If I use include() that won't work because it will copy all content, I only need the variable values on show.php not the entire page content. Make sense?
 
But I can't because I don't have the first name and last name value I only have the id (25). I need to access first name and last name on show.php. How can I do this?

the normal way (there are other methods) is to take the id and use it to query the database. as you are able to populate the select options, I assume you know how to do this.
 
The point jpadie is trying to make, is that the text of a select box or dropdown does not get sent in the POST variables, only its value.

You could have a judiciously applied Javascript script that takes the selected value text and sticks it in a hidden form field so it gets sent with the POST variables, or as jpadie suggests re-query the database based on the ID you are getting, and get your f_name and l_name values that way.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top