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

Post by Addos »

Hi,
Thanks for getting back.
“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.”
You see what I was really trying to do is have this all on one page and not use a second page at all. For example
If the fields are blank
Show ‘form’
if fields are filled and validate ok
don’t show form show thank you

If you copy and paste the complete code below you will see what is happening. If all the fields are correctly filled in the page submits but all the remaining code gets terminated ie the “Hi I should still see this after the form is submitted!” bit of text should still be there after I submit. This is the bit that is driving me nutters!
Thanks for your patience with me on this.
Brian


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 

&#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 »

edited code...

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>Set A Title Here</title>
</head> 
<body> 

<?PHP 
// Test whether the POST array has been set and makes certain 
// variables are initialized 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<br>";

&#125;&#125;&#125;&#125;&#125;
//Otherwise, display the form to user 
else &#123;
?> 

<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> 
<? &#125; ?>
Hi, I should now see this after the form is submitted! 
</body> 
</html>
that should do it now. :wink:
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks so much for this. But and there’s always a but! I had originally started out (many hours ago) using the ‘else’ clause as it seemed the obvious method to use but it stopped the validation script running and unfortunately this is the same with your version.
I’m still playing around with this but maybe you might see a way around this bit.
I owe you on this one.
Many 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 »

Addos wrote:but it stopped the validation script running and unfortunately this is the same with your version.
I don't get this bit. How does it stop validation script from running.
Can you tell me this prob in a bit detail and how could i reproduce this error.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Hi,
Sorry to confuse you on this. Basically I copied and pasted the revised version you posted and if I hit the submit button the form submits without checking for a valid email or blank fields.
Apologies if I have missed something obvious.
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 »

See you have put there the validation bit and it is running too but the prob is you haven't set any statements for outputting error messages. I thought you simply want to display confirmation if the mail was sent. If you want to put the error outputting thing you can use javascript for that or edit the current script and add error output.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks
When you say “you haven't set any statements for outputting error messages.” I’m not too sure I follow. If I was to simply remove the else clause that you popped in then the error message (that is already on the page) will run so do you mean that I should change them from their current location to somewhere else?
Ta again :wink:
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 »

output your error message by putting else clause just before last brace

Code: Select all

was successfully sent<br>"; 

&#125;&#125;&#125;&#125;&#125; 
    ^ Put Else Clause Before This Last Brace Here
//Otherwise, display the form to user 
else &#123;
and tell the user whatever went wrong with the submission.
Post Reply