Page 1 of 2
Passing form values question
Posted: Mon Feb 14, 2005 6:14 am
by Addos
I thought this should be obvious but I’m having trouble in passing a value from a page that has a form in it with a field called ‘name="lost_name" to another page that process the information. In my second page I have "Thank you" .$_POST['lost_name'] etc but obviously this is not enough as I’m getting the error “Undefined index: lost_name” so can anyone tell me (as a beginner) what I’m doing wrong?
Ta
Brian
Posted: Mon Feb 14, 2005 6:27 am
by Maugrim_The_Reaper
Check there's no 'id' attribute set - it may override the value of a 'name' attribute since 'name' is actually deprecated in xhtml1.0.
Any confusion between LOST name and LAST name?
A snippet of the code for html input and php code would be helpful to check anything else...

Posted: Mon Feb 14, 2005 6:49 am
by Addos
Thanks for replying so fast. I tried what you suggested but still no luck. I have posted both pages in full just so that you can see what I have been doing.
Thanks again
Brian
FORM PAGE
Code: Select all
<?PHP
// Test whether the POST array has been set and makes certain
// variables are initialzed with no content.
$pattern = '/^\wї-.\w]*@(ї-a-z0-9]+\.)+їa-z]{2,4}$/i';
if ($_POST && array_key_exists('sendCom',$_POST)) {
$nomessage='';
$error=array();
$error_email=array();
$message='';
$lost_email= $_POSTї'lost_email'];
// Check each field and build errors array if problems found
if (isset($_POSTї'lost_email']) && !empty($_POSTї'lost_email'])) {
$message=strip_tags($_POSTї'lost_email']);
}
else {
$nomessage = 'Email Required';
}
if (isset($_POSTї'lost_name']) && !empty($_POSTї'lost_name'])) {
$lost_name=trim($_POSTї'lost_name']);
}
else {
$errorї'lost_name'] = 'Name Required';
}
if (empty($_POSTї'lost_email'])) {// validation of email if inserted otherwise ignore
} else {
if (!preg_match($pattern,$lost_email)) $error_emailї'invalid'] = 'ERROR! Your email address seems to be invalid. <br> It should be similar to the following: info@me.com';
}
// If no errors, send email and redirect to acknowledgment page
if (!$nomessage && !$error) {
if (!$nomessage && !$error_email){
//mail($to,$subject,$message,$additional_headers);
header('Location: lostTEST_act.php');
exit();
} } } ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title> </title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" class="tbl_outer" id="tbl_outer"><tr><td class="mainContent">
</td>
</tr>
<tr>
<td id="error"><?php
if (isset($error_email)) {
foreach ($error_email as $key => $value) {
echo $value.'<br />';
}
}
?></td>
</tr>
<tr>
<td><form method="post" name="form1" action="<?php $_SERVERї'PHP_SELF']?>">
<table>
<tr valign="baseline">
<td align="right" nowrap class="error" id="error">* Name: <br>
<?php
if (isset($error)) { // Display error messages. Otherwise skip table row.
// Loop through error messages and display
foreach ($error as $key => $value) {
echo $value.'<br />';
}
}
?></td>
<td><input type="text" name="lost_name" id="" size="32" value="<?php if (isset($_POSTї'lost_name'])) echo $_POSTї'lost_name'];?>" ></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap class="error" id="error"> Membership number (if known):<br></td>
<td><input type="text" name="lost_membership_no" id="lost_membership_no" size="32" value="<?php if (isset($_POSTї'lost_membership_no'])) echo $_POSTї'lost_membership_no'];?>" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">*Email Address:
<?php if (isset($nomessage) && !empty($nomessage)) {
echo $nomessage; } else {
} ?>
</td>
<td><input type="text" name="lost_email" id="lost_email" value="<?php if (isset($_POSTї'lost_email'])) echo $_POSTї'lost_email'];?>" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">* Required Field </td>
<td><input name="sendCom" type="submit" id="sendCom" value="Post Message" />
<input name="Reset" type="reset" value="Reset"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p></td>
</tr>
</table>
</body>
</html>
SECOND ACT PAGE
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?PHP
echo "Thank you" . ' ' .$_POSTї'lost_name'] .',' . ' '."your message was successfully sent
//mail
?>
</body>
</html>
Posted: Mon Feb 14, 2005 7:03 am
by Chris Corbyn
Not too sure what the actual cause of the problem is until I look closer but one thing is staring back at me
Code: Select all
<?PHP
echo "Thank you" . ' ' .$_POSTї'lost_name'] .',' . ' '."your message was successfully sent
//mail
?>
You're missing the final quotes and semi-colon
Code: Select all
echo "Thank you" . ' ' .$_POSTї'lost_name'] .',' . ' '."your message was successfully sent ";
BTW... there's no need to concatenate so much when it's "string" . "string"
Code: Select all
echo "Thank you " .$_POSTї'lost_name'] .', '."your message was successfully sent";
Posted: Mon Feb 14, 2005 7:07 am
by Addos
Thanks for this. It was actually in the code I posted but I just clipped it out by accident when I was removing some of the mail stuff before I posted. Sorry for the confusion but it still doesn't work.
Ta
Brian
Posted: Mon Feb 14, 2005 7:44 am
by n00b Saibot
try putting
on the top of your page to see if the data is actually coming through.
Posted: Mon Feb 14, 2005 9:18 am
by Addos
Hi,
This is what I get at the mo.
Array ( )
Notice: Undefined index: lost_name in etc
Thanks
Brian
Posted: Mon Feb 14, 2005 9:23 am
by feyd
you are redirecting to this second page. The submitted data is likely to not pass through. Why not include it?
Posted: Mon Feb 14, 2005 9:27 am
by n00b Saibot
feyd wrote:
you are redirecting to this second page. The submitted data is likely to not pass through.
I hadn't noticed that before

. Must be gone dizzy
anyways, if you are redirecting to another page may be use session variable to pass the 'lost_name' variable
Posted: Mon Feb 14, 2005 9:33 am
by Addos
Thanks Guy’s
I had originally tried to include everything on the one page but ended tying myself in knots. I was using this at first:
Code: Select all
if (isset($error_email) && empty($error_email)) {
if (!empty($_POSTї'lost_email']) && !empty($_POSTї'lost_name'])) {
echo "Thank you" . ' ' .$_POSTї'lost_name'] .',' . ' '."your message was successfully sent and you will receive a new password via email within the next 24 hours.";
//Otherwise, display the form to user
exit();
} }
But exit(); was causing the script to terminate along with any remaining code on the page which was obviously not good. I’m not experienced enough at this but I thought that in the end it would be easier to just do the conditional statements on one page and then process the results on another although at the beginning I was aware that to economise an keep it to one page was really the way to go.
Appreciate your help.
Thanks
Brian
Posted: Mon Feb 14, 2005 9:42 am
by n00b Saibot
you can always group conditional outputs so that where you output that final thank-you and after exit() you don't have any more statements to be processed.
Posted: Mon Feb 14, 2005 6:50 pm
by Addos
Ok I have been at this no stop for 10 hours and it’s doin’ me head in.

I just can’t seem to find a way to place the conditional statements so that the remaining code in the page is not terminated using exit().
I have tried using header('Location: thanks.php'); exit(); but this is causing problems in that I already have made use of a similar header('Location) elsewhere in the site.
If anybody can put me out of my misery and correct this I will be eternally grateful as I thought that setting up a form in PHP would be a little easier than this even for a beginner like me.
Maybe there is some way I could include a redirect instead of header('Location: thanks.php'); …No?
Anyway many thanks for taking the time to read through this for me.
Brian
This is my feeble attempt!
Code: Select all
<?PHP
// Test whether the POST array has been set and makes certain
// variables are initialzed with no content.
$pattern = '/^\wї-.\w]*@(ї-a-z0-9]+\.)+їa-z]{2,4}$/i';
if ($_POST && array_key_exists('sendCom',$_POST)) {
$nomessage='';
$error=array();
$error_email=array();
$message='';
$lost_email= $_POSTї'lost_email'];
// Check each field and build errors array if problems found
if (isset($_POSTї'lost_email']) && !empty($_POSTї'lost_email'])) {
$message=strip_tags($_POSTї'lost_email']);
}
else {
$nomessage = 'Email Required';
}
if (isset($_POSTї'lost_name']) && !empty($_POSTї'lost_name'])) {
$lost_name=trim($_POSTї'lost_name']);
}
else {
$errorї'lost_name'] = 'Name Required';
}
if (empty($_POSTї'lost_email'])) {// validation of email if inserted otherwise ignore
} else {
if (!preg_match($pattern,$lost_email)) $error_emailї'invalid'] = 'ERROR! Your email address seems to be invalid. <br> It should be similar to the following: info@me.com';
}
// If no errors, send email and redirect to acknowledgment page
if (!$nomessage && !$error) {
if (!$nomessage && !$error_email){
//mail($to,$subject,$message,$additional_headers);
if (isset($error_email) && empty($error_email)) {
if (!empty($_POSTї'lost_email']) && !empty($_POSTї'lost_name'])) {
echo "Thank you" . ' ' .$_POSTї'lost_name'] .',' . ' '."your message was successfully sent";
exit();
//Otherwise, display the form to user
include('table.php');
}}}}}?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title> </title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" class="tbl_outer" id="tbl_outer"><tr><td class="mainContent">
</td>
</tr>
<tr>
<td id="error"><?php
if (isset($error_email)) {
foreach ($error_email as $key => $value) {
echo $value.'<br />';
}
}
?></td>
</tr>
<tr>
<td><form method="post" name="form1" action="<?php $_SERVERї'PHP_SELF']?>">
<table>
<tr valign="baseline">
<td align="right" nowrap class="error" id="error">* Name: <br>
<?php
if (isset($error)) { // Display error messages. Otherwise skip table row.
// Loop through error messages and display
foreach ($error as $key => $value) {
echo $value.'<br />';
}
}
?></td>
<td><input type="text" name="lost_name" id="" size="32" value="<?php if (isset($_POSTї'lost_name'])) echo $_POSTї'lost_name'];?>" ></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap class="error" id="error"> Membership number (if known):<br></td>
<td><input type="text" name="lost_membership_no" id="lost_membership_no" size="32" value="<?php if (isset($_POSTї'lost_membership_no'])) echo $_POSTї'lost_membership_no'];?>" ></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">*Email Address:
<?php if (isset($nomessage) && !empty($nomessage)) {
echo $nomessage; } else {
} ?>
</td>
<td><input type="text" name="lost_email" id="lost_email" value="<?php if (isset($_POSTї'lost_email'])) echo $_POSTї'lost_email'];?>" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">* Required Field </td>
<td><input name="sendCom" type="submit" id="sendCom" value="Post Message" />
<input name="Reset" type="reset" value="Reset"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p></td>
</tr>
</table>
Hi I should still see this after the form is submitted!
</body>
</html>
Posted: Tue Feb 15, 2005 1:48 am
by n00b Saibot
what output are you getting after trying this 'feeble attempt'

Posted: Tue Feb 15, 2005 2:51 am
by Addos
Hi,
Thanks for your reply.
Basically everything works in that I have no errors in running the validation of the code but once everything is in place and the page submits the exit() terminates any remaining code on the page. I have tried to find a way to place the statements as you kindly suggested earlier when you said “you can always group conditional outputs so that where you output that final thank-you and after exit() you don't have any more statements to be processed.” But I just haven’t managed to do this properly.
I even tried to use the action=go to another page with thank you message” but as this is below the exit() the page just submits without checking any of the fields.
Thanks again
Brian
Posted: Tue Feb 15, 2005 3:08 am
by n00b Saibot
I think basiclly you want that lost_name along with a bunch of variables (just guessing, since you haven't showed the mail sending part) accessible on the second thank-you page.
Addos wrote:I even tried to use the action=go to another page with thank you message
In the action part just add any many vars as you want and then you can access them using $_GET on the second page.