Jump to content

Archived

This topic is now archived and is closed to further replies.

skter4938

validation problems

Recommended Posts

Hello there.

I have a simple php email form. The form itself contacts the php page called contact.php and contact.php sends the mail. I'm sure people who have made an email script before know what I am talking about.

Well I have a validtaion section in the contact.php, and this is what it looks like:

[code]$validationOK=true;
if (checkEmail($EmailFrom) == FALSE){
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact/\">";
exit;
}
else if (Trim($Name)==""){
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact/\">";
exit;
}
else if (Trim($Subject)=="") {
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact/\">";
exit;
}
if (Trim($Message)==""){
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact/\">";
exit;
}
else if (!$validationOK) {
print "Email could not be sent due to errors. Please email the webmaster.";
exit;
}[/code]

Well instead of just refreshing back to the contact page, I want it to display text that says for example "The email you entered is not valid" next to the email textbox on my form. How can I do that?

Share this post


Link to post
Share on other sites
example:

pagethatprocessesform.php
[code]
<?php
session_start();

$name = $_POST['name'];
$email = $_POST['email'];

// alter your conditions as you see fit for error checking
if (trim($name) == '') {
  $error_msg[] = "no name entered.";
}
if(trim($email) == '') {
  $error_msg[] = "no email entered.";
}

if ($error_msg) {
   $_SESSION['error_msg'] = $error_msg;
   header ('Location: pagewithformonit.php');
   exit();
else {
  //process email
}
?>
[/code]

pagewithformonit.php
[code]
<?php
session_start();

if ($_SESSION['error_msg']) {
   echo "the following problems occured:<br>";
   foreach ($_SESSION['error_msg'] as $val) {
      echo $val . "<br>";
   }
   unset($_SESSION['error_msg']);
}

//code to show the form
?>
[/code]

Share this post


Link to post
Share on other sites
Thank you good sir [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

But there is one small problemo....

It gives me this error now

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/webtodes/public_html/contact/index.php:6) in /home/webtodes/public_html/contact/index.php on line 50[/quote]


and line 50 is [code]session_start();[/code] on the form page. I tried taking it out, and it just doesn't work.

Share this post


Link to post
Share on other sites
Is session_start(); called before [i]all[/i] page output?

Share this post


Link to post
Share on other sites
session_start(); needs to be called before any output (including HTML), its best to put it right at the top of your file, so that line 1 is "<?php session_start(); ?>".

Share this post


Link to post
Share on other sites
Ahhh Thank you :D

Everything works. Except when it writes "Please enter an email...blah blah blah...." It refreshes the form. Is there anyway of it just erasing what is wrong?

Share this post


Link to post
Share on other sites
in your pagethatprocessesform.php you need to pass the variables back to the form, using sessions, just like the error_msg variable

example:

pagethatprocessesform.php
[code]
<?php
session_start();

$name = $_POST['name'];
$email = $_POST['email'];

// alter your conditions as you see fit for error checking
if (trim($name) == '') {
  $error_msg[] = "no name entered.";
}
if(trim($email) == '') {
  $error_msg[] = "no email entered.";
}

if ($error_msg) {
   $_SESSION['error_msg'] = $error_msg;
  
   //create a session array with the values
   $_SESSION['form_values'] = array ('name' => $name, 'email' => $email);
   header ('Location: pagewithformonit.php');
   exit();
else {
  //process email
}
?>
[/code]

and then in pagewithformonit.php
[code]
<?php
session_start();

if ($_SESSION['error_msg']) {
   echo "the following problems occured:<br>";
   foreach ($_SESSION['error_msg'] as $val) {
      echo $val . "<br>";
   }
   unset($_SESSION['error_msg']);
}

?>
<!-- example of form -->
<form ...>
Name: <input type = 'text' name = 'name' value='<?= $_SESSION['form_values']['name'] ?><br>
Email: <input type = 'text' name = 'email' value='<?= $_SESSION['form_values']['email'] ?><br>
</form>

<?php
   unsest $_SESSION['form_values'];
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.