PHP form validation write var to form?

I've written the following code to ensure that the form has been completely filled out, and that a valid email address has been entered in the email box. The PHP loads HTML with an appropriate header. I kind of like this approach more than JavaScript. Is there any way that I can write the values of the form variables to the new .html page that loads so that the user can still see what they input?

<?php

      function check_email($email)
      {
            $result = TRUE;
            if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
            $result = FALSE;
      }
  return $result;
}

      //$jaime = "private@domain.com";
      //$kim = "email@domain.com";
      //$pat =  email2@domain.com";
      $subject = "Customer message";
      $first = $_POST['fname'];
      $last = $_POST['lname'];
      $from = $_POST['email'];
      $addy = $_POST['address'];
      $reason = $_POST['ts'];
      $message = $_POST['comments'];
      
      $c = 0;
      
      if (strlen($first) > 0)
      {
            $c++;
      }
      if (strlen($last) > 0)
      {
            $c++;
      }
      if (strlen($from) > 0)
      {
            $c++;
      }
      if (strlen($addy) > 0)
      {
            $c++;
      }
      if (strlen($reason) > 0)
      {
            $c++;
      }
      if (strlen($message) > 0)
      {
            $c++;
      }
      
      if ($c == 6) //all forms are filled out
      {
            
            $body = "From: $first $last\n E-Mail: $from\n Address: $addy\n Reason: $reason\n Message: $message";
            if (check_email($from) == false) //invalid email address
            {
                  header('Location: ../ive.html'); //invalid email address the only problem
            }
            else if (check_email($from) == true) //valid email address. confirmation page
            {
                  //mail($jaime, $subject, $body);
                  //mail($kim, $subject, $body);
                  //mail($pat, $subject, $body);
                  mail($abe, $subject, $body);
                  header('Location: ../thankyou.html');
            }
      }
      
      else if ($c<6)
      {
            if (strlen($from) > 0)
            {
                  if (check_email($from) == true) //valid email with incomplete form.
                  {
                        header('Location: ../cb.html');
                  }
                  if (check_email($from) == false) //invalid email address with incomplete form
                  {
                        header('Location: ../cbf.html');
                  }
            }
            if (strlen($from) == 0) //blank email address input
            {
                  header('Location: ../cb.html');
            }
      }

?>

Thanks
burnedfacelessAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xtermCommented:
Try
var_dump($_POST);

Open in new window

0
merwetta1Commented:
The easiest way to do what you want is to append the POST values to the query string of the URL used in the header redirects. However, the receiving pages will have to be PHP, so you can use the $_GET values to fill out the form.

<?
$q = '';
foreach($_POST as $k => $v)  $q .= ($q ? '&' : '').$k.'='.urlencode($v);

header('Location: ../cb.php?'.$q);
?>

Then you can fill in form fields like this in the receiving page:
<input name="first" value="<?php echo urldecode($_GET['first']); ?>">

However, this seems like an overly complicated approach. You might be better served by having one PHP script that has functions for displaying and verifying the form. You would use the verify function to create the customized html you want to show if there is an error, and then pass that html to the display function, which would show the custom html and the form with all previously POSTed values. This makes a nice tidy package where you can see all the things happening to your form in one place.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Here is my PHP email demo.  Save it as 'Email.php' and put in your own 'To' address.  On 'send' it will display all the info in a space above the form.  You change it so that it redirects on success but posts the data to be corrected on failure.
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

# some settings of POST vars
if (!isset($_POST['send']))  $send = ''; else $send = $_POST['send'];
if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];
if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText'];
if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText'];
if (!isset($_POST['ccText'])) $ccTexth = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText'];
if (!isset($_POST['nameText'])) $nameText = ''; else $nameText = $_POST['nameText'];
if (!isset($_POST['fromText'])) $fromText = ''; else $fromText = $_POST['fromText'];

if ($send == "") {
    $title="Test Email Page";
    $announce="---";
}
else {
	if($fromText === "") die("No name!");
  $toText="youremail@yourdomain.com";
	$title="Test Email Page";
  $announce="Your Message has been Sent!";
	$header = "From: ".$fromText."\r\n";
//	$header .= "Cc: ".$ccText."\n";
	$header .= "Reply-To : ".$fromText."\r\n";
	$header .= "Return-Path : ".$fromText."\r\n";
	$header .= "X-Mailer: PHP\r\n";
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
//	ini_set(sendmail_from,$fromText);  
	mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
//	ini_restore(sendmail_from);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php echo($title)?></title>
<style type="text/css">
<!-- 
A:link { color: #999999; }
A:visited { color: #999999; }
A:hover {color: #0099ff;}
-->
</style>
<script type="text/javascript">
<!--
function check()
{
var at=document.getElementById("fromText").value.indexOf("@");
var eml=document.getElementById("fromText").value;
var nam=document.getElementById("nameText").value;
var alerttxt="";
var submitOK="true";

if (eml.length < 5 || at == -1)
    {
    alerttxt=alerttxt+"Please enter a valid e-mail address!\r\n";
    submitOK="false"
    //return false;
    }
if (nam.length < 3)
    {
    alerttxt=alerttxt+"Please enter your name.\r\n";
    submitOK="false"
    //return false;
    }
if (submitOK=="false")
    {
    alert(alerttxt);
    return false;
    }

}
// -->
</script>
</head>

<body bgcolor="#ddeedd">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

<?php
if ($send != "") {
   	echo ("To: ".$toText."<br>\r\nSubject: ".$subjectText."<br>\r\n".$msgText."<br>\r\n".$header);
		}
?>

<p><b><font color="#000000" size="5">Test Email</font></b></p>
<font size="4" color="#000000">

<!--   <form method="POST" action="<?php echo($PHP_SELF)?>"> -->
<form method="POST" action="Email.php" onsubmit="return check();">
    <p><font size="3"><b>Name: <input type="text" name="nameText" id="nameText" size="46"></b></font></p>
    <p><font size="3"><b>Email: <input type="text" name="fromText" id="fromText" size="46"></b></font></p>
    <input type="hidden" name="subjectText" value="Web Mail">
    <p><font face="Arial" size="3"><b>Message Text:</b></font></p>
    <p><font face="Arial" size="3"><b><textarea rows="6" name="msgText" cols="60"></textarea></b></font></p>
    <p><font size="3"><b><input type="submit" value="Send" name="send" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
    <input type="hidden" name="state" value="1">
  </form>
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>

</font>
</td></tr>
</table> 
</div>


</body>
</html>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.