kevinkhan Posted February 17, 2010 Share Posted February 17, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/192398-php-to-js-problem/ Share on other sites More sharing options...
premiso Posted February 17, 2010 Share Posted February 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192398-php-to-js-problem/#findComment-1013779 Share on other sites More sharing options...
kevinkhan Posted February 17, 2010 Author Share Posted February 17, 2010 it seems to be very slow loading the page when there is errors. Java script wont speed this up no? Quote Link to comment https://forums.phpfreaks.com/topic/192398-php-to-js-problem/#findComment-1013784 Share on other sites More sharing options...
premiso Posted February 17, 2010 Share Posted February 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192398-php-to-js-problem/#findComment-1013790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.