Validate Function Not working

Hello!!

Thank you in advance for viewing my post. I have a PHP form that I am reusing. In the original format I had the validate function working, but when I paste in my new doc it does not work. The form works but if you leave fields blank the form still submits. I have gone over this code and just cannot find my typo or where the form is going wrong. I am a newbie to PHP. I have tried reading other posts on this same subject but cannot find any scripts that look like mine so I can compare and see where it is going wrong.

Any help would be greatly appreciated.

Thanks!
Sandra :slight_smile:

FORM HTML BELOW:

[code]

PAPERCUTZ :: Graphic Novel Publisher for Tweens & Teens

 

<li><a class="MenuBarItemSubmenu" href="#">COMICS HOME</a>
  <ul>
  <li><a href="http://www.papercutz.com/bionicle/index.html">BIONICLE</a></li>
  <li><a href="http://www.papercutz.com/classics/windwillow/willowhome.html">Classics Illus.</a></li>
  <li><a href="http://www.papercutz.com/disneyfairies/">Disney Fairies</a></li>
  <li><a href="http://www.papercutz.com/garfield/index.html">Garfield &amp; Co</a></li>
  <li><a href="http://www.papercutz.com/stilton/index.html">Geronimo Stilton</a></li>
  <li><a href="http://www.papercutz.com/hb/hb.html">Hardy Boys</a></li>
  <li><a href="http://www.papercutz.com/ninjago_ss/">LEGO NINJAGO</a></li>
  <li><a href="http://www.papercutz.com/nd/nd.html">Nancy Drew</a></li>
  <li><a href="http://www.papercutz.com/paperslices/slices_moreinfo.html">Papercutz Slices</a></li>
  <li><a href="http://www.papercutz.com/smurfs/">The Smurfs</a></li>
  <li><a href="http://www.papercutz.com/tc/tc.html">Tales from the Crypt</a></li>
  <li><a class="MenuBarItemSubmenu" href="#">Other Series</a>
      <ul>
  <li><a href="http://www.papercutz.com/new8x10/dance/">Dance Class</a></li>
  <li><a href="http://www.papercutz.com/new8x10/ernest/">Ernest</a></li>
  <li><a href="http://www.papercutz.com/new8x10/monster/">Monster</a></li>
  <li><a href="http://www.papercutz.com/new8x10/sybil/">Sybil</a></li>
  • NEWS
  • WHERE TO FIND
  • ABOUT
  •    <!-- #EndLibraryItem -->
           <p class="style2">&nbsp;</p>
    <p class="style2">&nbsp;</p>
    <p><a href="../images/LEGONINJAGOFLYER.pdf"><img src="../images/lego_contest_s1.jpg" width="661" height="106" alt="LEGO Ninjago Contest" /></a></p>
    <p><a href="../images/LEGONINJAGOFLYER.pdf">A Papercutz Promotion with Comicshops</a></p>
    <p>Showcase local kids' creativity! Award a great graphic novel prize package from Papercutz!</p>
    <p>Get free books for your store!</p>
    

    Download our LEGO Ninjago Comicshops Contest flyer for more information.

    A downloadable, customizable press release and community flyer will be available soon.

    If you are interested in participating, please fill out the form below.

    <p class="formP" >
     <label>Contact Name:</label>
      <span class="star">*</span>
      <input type="text" name="Name" id="Name" size="40" class="element text" />
      </p>
    <p class="formP"style="clear:both;"  >
      <label >Store Name:</label> 
      <span class="star">*</span>
        <input type="text" name="Storename" id="Storename" size="40" class="element text"  />
      </p>
    <p class="formP">
      <label>Street Address:</label>
       <span class="star">*</span>
       <input type="text" name="Address"id="Address" size="40"class="element text" /></p>
    <p class="formP"><label>City:</label>
    <span class="star">*</span>
      <input type="text" name="City" id="City" size="40" class="element text" />
    </p>
    <p class="formP">
      <label>State:</label>
      <span class="star">*</span>
        <input type="text" name="State"id="State" size="40" class="element text" /> 
      </p>
    <p class="formP">
      <label>Zip:</label>
      <span class="star">*</span>
      <input type="text" name="Zip"id="Zip" size="40"class="element text" />
    </p>
    <p class="formP">
      <label > Phone:</label>
      <span class="star">*</span>
        <input type="text" name="Phone"id="Phone" size="40"class="element text" /> 
    

    Email: *

     

            <p class="formP">
              <label class="longlabel">Please describe how you plan to promote this contest with co-op funds and other efforts.</label>
     </p>
      <p class="formP">&nbsp;</p>
      <p class="formP">
    


     

      

     

     

    VISIT OUR AUTHOR/EDITOR BLOG | SEND US YOUR COMMENTS!

    Papercutz • 40 Exchange Place, Suite 1308 • New York, NY 10005 • 800 886 1223

    LOOKING FOR ARTISTS!!

    Nancy Drew & The Hardy Boys are registered trademarks of Simon & Schuster, Inc.
    Disney Fairies © Disney Enterprises. All rights reserved.
    ©2006 Papercutz. All rights reserved.
    LEGO, the LEGO logo, BIONICLE, and the BIONICLE logo are trademarks of the LEGO Group. ” 2008 The LEGO Group. All Rights Reserved.
    Tales from the Crypt © 2008 William M. Gaines, Agent, Inc.
    Geronimo Stilton © Atlantyca S.p.A.

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    [/code]

    PHP Response code here:

    [code]<?php
    session_start();

    $errors = array ();
    $errors_msg = null;
    $input_arr = array();
    foreach ($_POST as $key => $input_arr) {
    $_POST[$key] = addslashes($input_arr);
    }
    $f_name = ($_POST[‘Name’]);
    $storename = ($_POST[‘Storename’]);
    $addr = ($_POST[‘Address’]);
    $city = ($_POST[‘City’]);
    $state = ($_POST[‘State’]);
    $zip = ($_POST[‘Zip’]);
    $phone = ($_POST[‘Phone’]);
    $em = ($_POST[‘Email’]);

    $describe = ($_POST[‘describe’]);

    $ms .="
    From: $f_name of $storename \n

    $f_name \n
    $storename \n
    $addr \n
    $city \n
    $state \n
    $zip \n
    $phone \n
    $em\n

    Please describe how you plan to promote this contest with co-op funds and other efforts. \n

    $describe ";

    $email="[email protected]";
    

    mail($email,‘Comic Shops Mailing List’.$_SERVER[‘remote_addr’], $ms); ?>

    PAPERCUTZ :: Graphic Novel Publisher for Tweens & Teens body { background-color: #FC3; }

     

     

     

    THANKS FOR JOINING OUR MAILING LIST!

     

     

    Download our LEGO Ninjago Comicshops Contest flyer for more information.

     

    A downloadable, customizable press release and community flyer will be available soon.

     

     

     

    VISIT OUR AUTHOR/EDITOR BLOG | SEND US YOUR COMMENTS!

    Papercutz • 40 Exchange Place, Suite 1308 • New York, NY 10005 • 800 886 1223

    LOOKING FOR ARTISTS!!

    Nancy Drew & The Hardy Boys are registered trademarks of Simon & Schuster, Inc.
    Disney Fairies © Disney Enterprises. All rights reserved.
    ©2006 Papercutz. All rights reserved.
    LEGO, the LEGO logo, BIONICLE, and the BIONICLE logo are trademarks of the LEGO Group. " 2008 The LEGO Group. All Rights Reserved.
    Tales from the Crypt © 2008 William M. Gaines, Agent, Inc.
    Geronimo Stilton © Atlantyca S.p.A.

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    [/code]

    A couple of things…I’m not going to preach on this too much, but to be honest it’s a really bad practice to rely entirely upon Javascript to handle form validation. All a user needs to do then to get around your validation scheme is turn off Javascript in their browser. So…add some validation on the server side in addition to what you’ve got going on in the JS.

    As to your JS…I might be missing something critical here, but I think your problem lies in the fact that you’re returning out of your validate_form function as soon as you check the first field. You only call validate_form the one time, (on form submit), and once you return out of a function, that function is done…it won’t keep going. So, what’s happening is your first field is validating, and none of the other fields are being checked assuming that one is good. You can confirm my theory by putting an invalid value in the Name field and seeing if it gives you your error.

    Assuming I’m correct, the solution is to get rid of all those returns and return just one time after all the fields have been checked. You can do that by setting up a variable that stores the state of the form depending upon whether or not all the fields are valid:

      <script>
        function validate_form(){
         var form_dirty = false
    
        if(firstName == "" || firstName == null){
          form_dirty = true; 
        }
    
       if(lastName == "" || lastName == null){
          form_dirty = true;
       }
       if(form_dirty){
         alert("There are problems with your form submission");
         return false;
       }
       return true:
      }
      </script>

    Sorry about the crap indentation, but you get the idea…

    Hope this helps!!

    THANK YOU so much for your time and reply. Honestly I didn’t realize there was another way to validate and what you said completely makes sense. I took this code from another form I created and that one works…still. This form was much short so I thought I would just modify it. Obviously I have left something out. I was looking at the code you sent which makes sense but I thought I actually did call into action all of the fields.

    Here is code snippet:

    [code]
    [/code]

    Doesn’t it go through each field under this function?

    function validate_form(thisform)

    It’s here where all the fields are listed. Did I do it wrong?

    Thank you again for your help!

    Sandra :slight_smile:

    Sponsor our Newsletter | Privacy Policy | Terms of Service