?
Solved

Problem using javascript with php to validate form fields

Posted on 2007-08-06
5
Medium Priority
?
309 Views
Last Modified: 2008-01-09
Hello,

I am trying to use JavaScript to validate a php form that I have created.  I have used a similar script before in an ASP form so I am a bit perplexed as to why it is not working correctly. The script seems to check the first field to see if it has been filled out or not but the form continues to submit and post the date to the next page which sends an email with the data. So, if I leave the first field blank I am alerted to fill it the field but as soon as I click "ok" in the alert box the form continues to process and sends the email. Here is the code that I am using:





<script language="javascript">
<!-- Code after this will be ignored by older browsers

function formChecker()
{
if (document.contactForm.firstname.value=="") {
                  alert("Please tell us your first name.");
                  contactForm.firstname.focus();
                  return false;
                  
                  }else {
                  event.returnValue=true;
                  }

if (document.contactForm.lasttname.value=="") {
                  alert("Please fill tell us your last name.");
                  contactForm.lastname.focus();
                  return false;
                  
                  }else {
                  event.returnValue=true;
                  }
                                    


if (document.contactForm.daytime_phone.value=="") {
                  alert("Please fill tell us your first name.");
                  contactForm.daytime_phone.focus();
                  return false;
                  
                  }else {
                  event.returnValue=true;
                  }
                  
if (document.contactForm.customer_email.value=="") {
                  alert("Please tell us your email address.");
                  contactForm.customer_email.focus();
                  return false;
                  
                  }else {
                  event.returnValue=true;
                  }

if (document.contactForm.customer_address.value=="") {
                  alert("Please tell us your home address.");
                  contactForm.customer_address.focus();
                  return false;
                  
                  }else {
                  event.returnValue=true;
                  }
if (document.contactForm.city.value=="") {
                  alert("Please tell us your city.");
                  contactForm.city.focus();
                  return false;
                  
                  }else {
                  event.returnValue=true;
                  }

if (document.contactForm.zip.value=="") {
                  alert("Please tell us your city.");
                  contactForm.zip.focus();
                  return false;
                  
                  }else {
                  event.returnValue=true;
                  }
                  
      
      
//Stop hiding code here-->
}</script>



</head>

<body>


<form action="contactform_process.php" method="post" name="contactForm" id="contactForm" onSubmit="return formChecker();" >
   
      <div class="formfield">Type of Work:<select name="estimateType" id="estimateType">
                                    <option value="">Select the type of work..</option>
                                    <option value="Repair">Repair</option>
                                    <option value="Remodel">Remodel</option>
                                    <option value="Design">Design</option>
      </select></div>
      <div class="formfield">First Name:<input type="text" alt="First Name"  name="firstname"  /></div>
      <div class="formfield">Last Name:<input type="text" alt="Last Name"  name="lastname"  /></div>
      <div>Daytime Phone:<input type="text" alt="Daytime Phone" name="phone" /></div>
      <div>Email:<input type="text" alt="Email" name="email" /></div>
      <div>Address:<input type="text" alt="Home Address" name="address" /></div>
      <div>City:<input type="text" alt="City" name="city" /></div>
      <div>Zip Code:<input type="text" alt="Zip Code" name="zip"/></div>       
      <div><input type="submit"  name="submit" value="Submit"  />      </div>
      <div><input type=reset name=reset value="Reset"></div>
      <input type="hidden" name="subject" value="Form Submission" />
      <!--<input type="hidden" name="redirect" value="thankyou.php" />-->
             
      </form>
<?php
      
      echo date("l, F d, Y h:i" ,time());

?>

</body>
</html>


Thanks!
0
Comment
Question by:dstrong23
5 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 19643469
There are a lot of Java script error in your validation. Try this as validation (and fix the alter messages):

unction formChecker()
{
if (document.contactForm.firstname.value=="") {
                  alert("Please tell us your first name.");
                  contactForm.firstname.focus();
                  return false;
                  }

if (document.contactForm.lastname.value=="") {
                  alert("Please fill tell us your last name.");
                  contactForm.lastname.focus();
                  return false;
                  }

if (document.contactForm.phone.value=="") {
                  alert("Please fill tell us your first name.");
                  contactForm.phone.focus();
                  return false;
                  }

if (document.contactForm.email.value=="") {
                  alert("Please tell us your email address.");
                  contactForm.email.focus();
                  return false;
                  }

if (document.contactForm.address.value=="") {
                  alert("Please tell us your home address.");
                  contactForm.address.focus();
                  return false;
                  }
if (document.contactForm.city.value=="") {
                  alert("Please tell us your city.");
                  contactForm.city.focus();
                  return false;
                  }

if (document.contactForm.zip.value=="") {
                  alert("Please tell us your city.");
                  contactForm.zip.focus();
                  return false;
                  }

return true;

//Stop hiding code here-->
}</script>
0
 
LVL 19

Expert Comment

by:Michael701
ID: 19643476
take out all the else's, and only return true if you pass all of them;

if (document.contactForm.firstname.value=="") {
                  alert("Please tell us your first name.");
                  contactForm.firstname.focus();
                  return false;
                  }

if (document.contactForm.lasttname.value=="") {
                  alert("Please fill tell us your last name.");
                  contactForm.lastname.focus();
                  return false;
                  }

if (document.contactForm.daytime_phone.value=="") {
                  alert("Please fill tell us your first name.");  ************* change to phone number
                  contactForm.daytime_phone.focus();
                  return false;
                 }
......
return true;  *************** at same level as if's, NOT part of any 'else' statement

//Stop hiding code here-->
}</script>
               
           
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1000 total points
ID: 19643538
And lastly pass the form - it is not good to have contactform. one place and document.contactform another.
If you pass the form object to the function (this) you can use whatever variable name and you do not need to name the form itself:

<html>
<head>
<script language="javascript">
<!-- Code after this will be ignored by older browsers

function formChecker(theForm) {
  if (theForm.estimateType.selectedIndex<1) {
    alert("Please choose an estimate type.");
    theForm.estimateType.focus();
    return false;
  }
  if (theForm.firstname.value=="") {
    alert("Please tell us your first name.");
    theForm.firstname.focus();
    return false;
  }

  if (theForm.lastname.value=="") {
    alert("Please fill tell us your last name.");
    theForm.lastname.focus();
    return false;
  }
  if (theForm.phone.value=="") {
    alert("Please tell us your phone number.");
    theForm.phone.focus();
    return false;
  }
  if (theForm.email.value=="") {
    alert("Please tell us your email address.");
    theForm.email.focus();
    return false;
  }
  if (theForm.address.value=="") {
    alert("Please tell us your home address.");
    theForm.address.focus();
    return false;
  }
  if (theForm.city.value=="") {
    alert("Please tell us your city.");
    theForm.city.focus();
    return false;
  }
  if (theForm.zip.value=="") {
    alert("Please tell us your zip.");
    theForm.zip.focus();
    return false;
  }
  return true; // all fields passed                
}      
     
//Stop hiding code here-->
</script>
</head>

<body>


<form action="contactform_process.php" method="post"
onSubmit="return formChecker(this);" >
   
      <div class="formfield">Type of Work:<select name="estimateType" id="estimateType">
                                    <option value="">Select the type of work..</option>
                                    <option value="Repair">Repair</option>
                                    <option value="Remodel">Remodel</option>
                                    <option value="Design">Design</option>
      </select></div>
      <div class="formfield">First Name:<input type="text" alt="First Name"  name="firstname"  /></div>
      <div class="formfield">Last Name:<input type="text" alt="Last Name"  name="lastname"  /></div>
      <div>Daytime Phone:<input type="text" alt="Daytime Phone" name="phone" /></div>
      <div>Email:<input type="text" alt="Email" name="email" /></div>
      <div>Address:<input type="text" alt="Home Address" name="address" /></div>
      <div>City:<input type="text" alt="City" name="city" /></div>
      <div>Zip Code:<input type="text" alt="Zip Code" name="zip"/></div>      
      <div><input type="submit"  name="submit" value="Submit"  />      </div>
      <div><input type=reset name=reset value="Reset"></div>
      <input type="hidden" name="subject" value="Form Submission" />
      <!--<input type="hidden" name="redirect" value="thankyou.php" />-->
             
      </form>
<?php
     
      echo date("l, F d, Y h:i" ,time());

?>

</body>
</html>
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19644423
corrections to your javascript have already been suggested, but I highly recommend that you do the check in your contactform_process.php independently of what javascript might do. JavaScript is client-side, can be switched off, or can be manipulated to whatever you want.
0
 

Author Comment

by:dstrong23
ID: 19647056
Thanks for all of your help!  Removing the "else if" alone did not fix my problem. The form finally worked correctly once I added mplungjan's suggestion.

Thanks again!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question