Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/12470-validation-problems/
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]
Link to comment
https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47720
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.
Link to comment
https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47817
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]
Link to comment
https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47914
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.