Jump to content

Form Validation


ornclo

Recommended Posts

I am asking for some simple help, please. :-)

 

I have a form that I would like to validate very specific information on - without having it validate in the php and loose what the client just put into the site.

 

I have found an example in this "validation" process.  However, when I open this up in Dreamweaver CS4, I have an error on line 67 as a syntex error. I am unable to see where this would be.

 

All I need is a verification on the name, the phone number, and the email address.

 

Here is what I have so far....

 

**This is contact form**

 

<script language="JavaScript">
  
  function checkForm() {
   var cname, cemail, cphone
   with(window.document.Contact1) {
    cname    = name;
    cemail   = EmailFrom;
    cphone   = phone;
   }

   if(trim(cname.value) == '') {
    alert('Please enter your name');
    cname.focus();
    return false;
   }

   else if(trim(cemail.value) == '') {
    alert('Please enter your email');
    cemail.focus();
    return false;
   }

   else if(!isEmail(trim(cemail.value))) {
    alert('Email address is not valid');
    cemail.focus();
    return false;
   }

   else if(trim(cphone.value) == '') {
      alert('Please enter message subject');
      csubject.focus();
      return false;
   }
   
  }
  else {
   cname.value    = trim(cname.value);
   cemail.value   = trim(cemail.value);
   cphone.value   = trim(cphone.value);
   return true;
  }
}

function trim(str) {
  return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(str) {
  var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|
   ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|
   bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|
   ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|
   dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|
   gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|
   hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|
   kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|
   ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|
   mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|
   nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|
   re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|
   su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|
   ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|
   zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
   
   return regex.test(str); 
  }
   
</script>  

 

Below is the php file that I am using....

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
   
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  
  <title>Emailing Form Data</title>
  
</head>

<body>

<?php 
   
  ini_set ("display_errors", "1");
  error_reporting(E_ALL);

  //declaration of variables

  $EmailFrom = trim(stripslashes($_POST['EmailFrom'])); 
  $EmailTo =   "ornclo@aol.com";
  $Subject =   "Women United Contact";
  
  $Name =     trim(stripslashes($_POST['name']));
  $Address =  trim(stripslashes($_POST['address']));
  $City =     trim(stripslashes($_POST['city']));
  $State =    trim(stripslashes($_POST['state']));
  $Phone1 =   trim(stripslashes($_POST['phone1']));
  $Phone2 =   trim(stripslashes($_POST['phone2']));
  $Phone3 =   trim(stripslashes($_POST['phone3']));
  $email  =   trim(stripslashes($_POST['email']));
    
  foreach ($_POST['visit'] as $value) {
   $Church =   trim(stripslashes($value));
  }
  
  foreach ($_POST['hear'] as $hear) {
   $About =    trim(stripslashes($hear));
  }
  
  foreach ($_POST['help'] as $help) { 
   $Ask =      trim(stripslashes($help));
  }
  
  $Comments = trim(stripslashes($_POST['comments'])); 


  // validation

  $validationOK=true;

   if (($EmailFrom)=="") $validationOK=false;
   if (($Name)=="")      $validationOK=false;
   if (($Phone1)=="")    $validationOK=false;
   if (($Phone2)=="")    $validationOK=false;
   if (($Phone3)=="")    $validationOK=false;
   if (($email)=="")     $validationOK=false;
      
   if (($Comments)=="")  $validationOK=false;
   
   if (!$validationOK) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=WUerror.html\">";
    exit;
   }


   // prepare email body text

   $Body = "\n";
   $Body .= "Name: ";
   $Body .= $Name;
   $Body .= "\n";
   $Body .= "Address: ";
   $Body .= $Address;
   $Body .= "\n";
   $Body .= "City: ";
   $Body .= $City;
   $Body .= "\n";
   $Body .= "State: ";
   $Body .= $State;
   $Body .= "\n";
   $Body .= "Phone: ";
   $Body .= $Phone1;
   $Body .= "-";
   $Body .= $Phone2;
   $Body .= "-";
   $Body .= $Phone3;
   $Body .= "\n";
   $Body .= "Attended Church of God?: ";
   $Body .= $Church;
   $Body .= "\n";
   $Body .= "How did you hear about us?: ";
   $Body .= $About;
   $Body .= "\n";
   $Body .= "What can we do to help you?: ";
   $Body .= $Ask;
   $Body .= "\n";
   $Body .= "Comments: ";
   $Body .= $Comments;
   $Body .= "\n";


   // send email 

   $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");


   // redirect to success page 

   if ($success){
    print "<meta http-equiv=\"refresh\" content=\"0;URL=WUok.html\">"; 
   }
   else{
    print "<meta http-equiv=\"refresh\" content=\"0;URL=WUerror.html\">";
   }

  ?>

</body>

</html>

 

 

Unless there is a way to change this line:

 

print "<meta http-equiv=\"refresh\" content=\"0;URL=WUerror.html\">";

 

to have it keep in the same window - or even possibly open a NEW window with the "error.html" - that would be fine as well.

 

 

Again - when I open up Contact1.html in DW CS4, I get a syntext error on line 67.

When I use just straight form, it opens to new window with php - > error.html, which in turns looses everything the client just typed.

 

Any help is greatly appreciated and thank you in advance.

 

Blessings!

 

If asked, I can attach the files if needed as well, but the code is pretty self-explain.

Link to comment
Share on other sites

okay,

so I got it fixed in the form part.

 

In the example that I have that tells me how the validation works, it is in the same form.

How do you make the php "secret" if it is in the same form?

 

I am still learning how this php all works, so forgive my ignorance.

 

Thank you for the help at least on the error, you where correct.

Still doesn't work entirely, but at least there is no syntex error.

 

Again, any help would be greatly appreciated.

Link to comment
Share on other sites

If by making PHP "secret" you mean hiding it from the end users then no worries, PHP is pre-processed so once your page loads if someone tries to see the source they won't see any PHP, just HTML.

 

If you mean removing the processing part of the PHP from that file, then a simple require_once would suffice.

 

Just put all that processing PHP in another file and save it, for example, processing.php and then on the form page at  the beginning add this :

 

<?php require_once('processing.php'); ?> 

Link to comment
Share on other sites

Maybe I'm missing something, but is there a reason you're using foreach loops for some of the $_POST variables? For example, this

 

<?php
//...

foreach ($_POST['visit'] as $value) {
     $Church =   trim(stripslashes($value));
}

//...
?>

 

 

should be the same as:

 

<?php
//...

$Church =   trim(stripslashes($_POST['visit']));

//...
?>

Link to comment
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.