Passing form values question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Passing form values question

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...:)
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post 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&#1111;-.\w]*@(&#1111;-a-z0-9]+\.)+&#1111;a-z]&#123;2,4&#125;$/i';
if ($_POST && array_key_exists('sendCom',$_POST)) &#123;
  $nomessage='';
  $error=array();
  $error_email=array();
  $message='';
  $lost_email= $_POST&#1111;'lost_email'];
   // Check each field and build errors array if problems found
if (isset($_POST&#1111;'lost_email']) && !empty($_POST&#1111;'lost_email'])) &#123;
  $message=strip_tags($_POST&#1111;'lost_email']);
  &#125;
else &#123;
  $nomessage = 'Email Required';
  &#125;
if (isset($_POST&#1111;'lost_name']) && !empty($_POST&#1111;'lost_name'])) &#123;
  $lost_name=trim($_POST&#1111;'lost_name']);
  &#125;
else &#123;
  $error&#1111;'lost_name'] = 'Name Required';
  &#125;
  if (empty($_POST&#1111;'lost_email'])) &#123;// validation of email if inserted otherwise ignore
	 &#125; else &#123;
	 if (!preg_match($pattern,$lost_email)) $error_email&#1111;'invalid'] = 'ERROR! Your email address seems to be invalid. <br> It should be similar to the following: info@me.com';
     &#125;
// If no errors, send email and redirect to acknowledgment page
if (!$nomessage && !$error) &#123;
if (!$nomessage && !$error_email)&#123;
  //mail($to,$subject,$message,$additional_headers);
  header('Location: lostTEST_act.php');
  exit();
&#125; &#125; &#125; ?>

<!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)) &#123;	  
		foreach ($error_email as $key => $value) &#123;
		  echo $value.'<br />';
		  &#125;
		  &#125;
		?></td>
 </tr>
 <tr>
    <td><form method="post" name="form1" action="<?php $_SERVER&#1111;'PHP_SELF']?>">
        <table>
          <tr valign="baseline">
            <td align="right" nowrap class="error" id="error">* Name: <br>
			<?php 
	  if (isset($error)) &#123; // Display error messages. Otherwise skip table row.
	   // Loop through error messages and display
		foreach ($error as $key => $value) &#123;
		  echo $value.'<br />';
		  &#125;
		  &#125;
		?></td>	
            <td><input type="text" name="lost_name" id="" size="32" value="<?php if (isset($_POST&#1111;'lost_name'])) echo $_POST&#1111;'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&#1111;'lost_membership_no'])) echo $_POST&#1111;'lost_membership_no'];?>" ></td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">*Email Address:
			<?php if (isset($nomessage) && !empty($nomessage)) &#123;
		  echo $nomessage; &#125; else &#123; 		 
		  &#125; ?>
			</td>
			<td><input type="text" name="lost_email" id="lost_email" value="<?php if (isset($_POST&#1111;'lost_email'])) echo $_POST&#1111;'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>&nbsp;</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&#1111;'lost_name'] .',' . ' '."your message was successfully sent 
	//mail
	
?>
</body>
</html>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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&#1111;'lost_name'] .',' . ' '."your message was successfully sent 
   //mail 
    
?>
You're missing the final quotes and semi-colon

Code: Select all

echo "Thank you" . ' ' .$_POST&#1111;'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&#1111;'lost_name'] .', '."your message was successfully sent";
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

try putting

Code: Select all

print_r($_POST);
on the top of your page to see if the data is actually coming through.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Hi,
This is what I get at the mo.

Array ( )
Notice: Undefined index: lost_name in etc

Thanks
Brian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you are redirecting to this second page. The submitted data is likely to not pass through. Why not include it?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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 :x . Must be gone dizzy :D

anyways, if you are redirecting to another page may be use session variable to pass the 'lost_name' variable
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post 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)) &#123;
  if (!empty($_POST&#1111;'lost_email']) && !empty($_POST&#1111;'lost_name'])) &#123;
    echo "Thank you" . ' ' .$_POST&#1111;'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(); 
&#125; &#125;
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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Ok I have been at this no stop for 10 hours and it’s doin’ me head in. :cry: 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
:wink:

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&#1111;-.\w]*@(&#1111;-a-z0-9]+\.)+&#1111;a-z]&#123;2,4&#125;$/i'; 
if ($_POST && array_key_exists('sendCom',$_POST)) &#123; 
  $nomessage=''; 
  $error=array(); 
  $error_email=array(); 
  $message=''; 
  $lost_email= $_POST&#1111;'lost_email']; 
   // Check each field and build errors array if problems found 
if (isset($_POST&#1111;'lost_email']) && !empty($_POST&#1111;'lost_email'])) &#123; 
  $message=strip_tags($_POST&#1111;'lost_email']); 
  &#125; 
else &#123; 
  $nomessage = 'Email Required'; 
  &#125; 
if (isset($_POST&#1111;'lost_name']) && !empty($_POST&#1111;'lost_name'])) &#123; 
  $lost_name=trim($_POST&#1111;'lost_name']); 
  &#125; 
else &#123; 
  $error&#1111;'lost_name'] = 'Name Required'; 
  &#125; 
  if (empty($_POST&#1111;'lost_email'])) &#123;// validation of email if inserted otherwise ignore 
    &#125; else &#123; 
    if (!preg_match($pattern,$lost_email)) $error_email&#1111;'invalid'] = 'ERROR! Your email address seems to be invalid. <br> It should be similar to the following: info@me.com'; 
     &#125; 
// If no errors, send email and redirect to acknowledgment page 
if (!$nomessage && !$error) &#123; 
if (!$nomessage && !$error_email)&#123; 
  //mail($to,$subject,$message,$additional_headers); 
if (isset($error_email) && empty($error_email)) &#123;
if (!empty($_POST&#1111;'lost_email']) && !empty($_POST&#1111;'lost_name'])) &#123;
echo "Thank you" . ' ' .$_POST&#1111;'lost_name'] .',' . ' '."your message was successfully sent";
exit(); 

//Otherwise, display the form to user
include('table.php');

&#125;&#125;&#125;&#125;&#125;?>

<!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)) &#123;     
      foreach ($error_email as $key => $value) &#123; 
        echo $value.'<br />'; 
        &#125; 
        &#125; 
      ?></td> 
 </tr> 
 <tr> 
    <td><form method="post" name="form1" action="<?php $_SERVER&#1111;'PHP_SELF']?>"> 
        <table> 
          <tr valign="baseline"> 
            <td align="right" nowrap class="error" id="error">* Name: <br> 
         <?php 
     if (isset($error)) &#123; // Display error messages. Otherwise skip table row. 
      // Loop through error messages and display 
      foreach ($error as $key => $value) &#123; 
        echo $value.'<br />'; 
        &#125; 
        &#125; 
      ?></td>    
            <td><input type="text" name="lost_name" id="" size="32" value="<?php if (isset($_POST&#1111;'lost_name'])) echo $_POST&#1111;'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&#1111;'lost_membership_no'])) echo $_POST&#1111;'lost_membership_no'];?>" ></td> 
          </tr> 
          <tr valign="baseline"> 
            <td nowrap align="right">*Email Address: 
         <?php if (isset($nomessage) && !empty($nomessage)) &#123; 
        echo $nomessage; &#125; else &#123;        
        &#125; ?> 
         </td> 
         <td><input type="text" name="lost_email" id="lost_email" value="<?php if (isset($_POST&#1111;'lost_email'])) echo $_POST&#1111;'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>&nbsp;</p></td> 
  </tr> 
</table> 
Hi I should still see this after the form is submitted!
</body> 
</html>
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

what output are you getting after trying this 'feeble attempt' :?:
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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.
Post Reply