Jump to content


Photo

validation problems


  • Please log in to reply
7 replies to this topic

#1 skter4938

skter4938
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 20 June 2006 - 03:40 PM

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:

$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;
}

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?

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 20 June 2006 - 04:22 PM

example:

pagethatprocessesform.php
<?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
}
?>

pagewithformonit.php
<?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
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 skter4938

skter4938
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 20 June 2006 - 10:32 PM

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
session_start();
on the form page. I tried taking it out, and it just doesn't work.

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 June 2006 - 10:35 PM

Is session_start(); called before all page output?

#5 skter4938

skter4938
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 20 June 2006 - 10:36 PM

I put session_start(); right before the rest of the php code.

#6 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 June 2006 - 10:37 PM

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(); ?>".

#7 skter4938

skter4938
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 20 June 2006 - 10:39 PM

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?

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 June 2006 - 04:14 AM

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
<?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
}
?>

and then in pagewithformonit.php
<?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']; 
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users