Jump to content

PHP to JS problem


kevinkhan

Recommended Posts

Hi guys.. I really need a bit of help.. is anyone good looking at this good with JS?

 

I have a php form validation script but i think its a bit slow and would rather a JS script instead...

 

here is what i have in php..

 

	<?php
  if(isset($_POST['submit']))
      {
          $firstName = $_POST['firstName'];
          $lastName = $_POST['lastName'];
          $email = $_POST['email'];
          $mobile = $_POST['mobile'];
          $comments = $_POST['comments'];
         
          $errors = array();
          
        function display_errors($error)
          {
          echo "<p class=\"formMessage\">";
          
          
            echo $error[0];
            
            echo "</p>";
          }  
          
         function validateNames($names) 
        {
        return(strlen($names) < 3);
        } 
        
        function validateEmail($strValue) 
        {
           $strPattern = '/([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})/sim';
           return(preg_match($strPattern,$strValue));
        } 
        
        function validateMobile($strValue) 
        {
           $strPattern = '/^\d{10}$/';
           return(preg_match($strPattern,$strValue));
        } 
        
        function validateComments($comments) 
        {
        return(strlen($comments) < 10);
        } 
        
        if(validateNames($firstName)) 
        {    
        $errors[] = 'Please Enter Your First Name';    
        }
        
         if(validateNames($lastName)) 
        {    
        $errors[] = 'Please Enter Your Second Name';    
        }
        
          
        if(!validateEmail($email)) 
        {    
        $errors[] = 'Please Enter Your Correct Email';    
        }
        
        if(!validateMobile($mobile)) 
        {    
        $errors[] = 'Please Enter Your Correct Mobile Number';    
        }
        
        if(validateComments($comments)) 
        {    
        $errors[] = 'Please Enter A Comment More Than 10 Characters';    
        }


          if(empty($errors))
          {
            $to = "info@eventpromotion.ie";
            $subject = "Event Promotion Enquiry!";
            $body = 
             "First Name: " . $_POST['firstName'] .
             "\nLast Name: " . $_POST['lastName'] .
             "\nEmail: " . $_POST['email'] .  
        	   "\nMobile: " . $_POST['mobile'] . 
        	 "\nMessage: " . $_POST['comments'];
        	  $headers = "From: ". $firstName ." ". $lastName . " <" . $email . ">\r\n";


          if (mail($to, $subject, $body, $headers)) {
            echo("<p class=\"formMessage\">Thanks for submitting your enquiry.</p>");
            }
            else
            {
            echo("<p class=\"formMessage\">Message delivery failed.</p>");
            }
          }
          else
          {
          //echo "error";
         display_errors($errors);
          }    
      }                   
?>
<form id="form" method="post" action="index.php#quickContact"> 
              <p> 
                <label>First Name</label><br /> 
                <input type="text" name="firstName" value="<?php if(isset($firstName)){echo $firstName;} ?>" />      
            </p> 
            <p> 
                <label>Last Name</label><br /> 
                <input type="text" name="lastName" value="<?php if(isset($lastName)){echo $lastName;} ?>" />      
            </p> 
              <p> 
                <label>Email:</label><br /> 
                <input type="text" name="email" value="<?php if(isset($email)){echo $email;} ?>" /> 
                
            </p>               
            <p> 
                <label>Mobile:</label><br /> 
                <input type="text" name="mobile" value="<?php if(isset($mobile)){echo $mobile;} ?>" /> 
                
            </p>                                  
            <p> 
                <label>Comments:</label> <br />
                <textarea name="comments" cols="30" rows="3" ><?php if(isset($comments)){echo $comments;} ?></textarea>    
            </p> 
            <p>    <input class="send" type="image" src="images/submit2.gif" name="submit" value="Submit"  /></p> 
  </form>   

 

does anyone know how to transfer this to JS so that it will be easy to understand.. Im not good with JS at all :(

Link to comment
Share on other sites

The only reason it would be slow is possibly from the mail server, which it would be if you are using SMTP to connect to it.

 

JS will not solve the "slow" problem and the main issue with validating in Javascript is that Javascript can be turned off and thus the user has circumvented your validation all together. The code itself looks fine, given that I highly doubt that incorporating javascript validation will speed it up. 

Link to comment
Share on other sites

I do not see why it would be loading slow when an error is encountered, as you are not doing any loops and the regex is minimal.

 

As far as JS speeding it up, it may, but as I stated before using JS you risk someone turning off JS and bypassing all of your validation, which means invalid data can easily get through.

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.