Passing form values question
Moderator: General Moderators
Passing form values question
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
Ta
Brian
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
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
SECOND ACT PAGE
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>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>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Not too sure what the actual cause of the problem is until I look closer but one thing is staring back at me
You're missing the final quotes and semi-colon
BTW... there's no need to concatenate so much when it's "string" . "string"
Code: Select all
<?PHP
echo "Thank you" . ' ' .$_POSTї'lost_name'] .',' . ' '."your message was successfully sent
//mail
?>Code: Select all
echo "Thank you" . ' ' .$_POSTї'lost_name'] .',' . ' '."your message was successfully sent ";Code: Select all
echo "Thank you " .$_POSTї'lost_name'] .', '."your message was successfully sent";- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
try putting
on the top of your page to see if the data is actually coming through.
Code: Select all
print_r($_POST);- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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:
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
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();
} }Appreciate your help.
Thanks
Brian
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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!
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>- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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
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
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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.
In the action part just add any many vars as you want and then you can access them using $_GET on the second page.Addos wrote:I even tried to use the action=go to another page with thank you message