Search the Community
Showing results for tags 'strlen'.
-
I am trying to confirm entries in the form below using strlen but am stymied by a strange bug/error - in check for entries in subject and message fields else if((strlen($_POST['subject']) == 0) || (strlen($_POST['message']) == 0)) the first condition works perfectly ($length=0) but strlen($_POST['message'] returns $messagelen=5 or more even when nothing is entered?! When you first click in the textarea the cursor positions beyond the start point - which is probably causing the error, but why? Thank you for any help. <?php function spamcheck($field) { //filter_var() sanitizes the e-mail //address using FILTER_SANITIZE_EMAIL $field=filter_var($field, FILTER_SANITIZE_EMAIL); //filter_var() validates the e-mail //address using FILTER_VALIDATE_EMAIL if(filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } } if (isset($_POST['email'])) {//if "email" is filled out, proceed //check if the email address is invalid $mailcheck = spamcheck($_POST['email']); if ($mailcheck==FALSE) { $url = htmlspecialchars($_SERVER['HTTP_REFERER']); echo "<a href='$url'>Invalid form entry.<br><br> Please click here to return to form and retry.</a>"; } // check for entries in subject and message fields else if((strlen($_POST['subject']) == 0) || (strlen($_POST['message']) == 0)) { $url = htmlspecialchars($_SERVER['HTTP_REFERER']); $length = strlen($_POST['subject']); $messagelen = strlen($_POST['message']); echo "<a href='$url'>Please fill in all fields. " . $length . $messagelen . "<br><br> Please click here to return to form and retry.</a>"; } else {//send email $email = $_POST['email'] ; $subject = $_POST['subject'] ; $message = $_POST['message'] ; mail("me@example.com", "Subject: $subject", $message, "From: $email" ); $url = htmlspecialchars($_SERVER['HTTP_REFERER']); echo "Thank you for using our mail form"; echo "<a href='$url'>Click here to return</a>"; } } else {//if "email" is not filled out, display the form echo "<p><b>If you have any questions and wish to contact us,<br /> please fill in the form below.</b></p><br> <form method='post' action='formpage.php'> <fieldset><legend>Your Contact Information</legend> Email: <input name='email' type='text' tabindex='1'><br> Subject: <input name='subject' type='text' tabindex='2'><br> Message:<br> <textarea name='message' rows='7' cols='40' tabindex='3'> </textarea><br> <input type='submit' id='submit' tabindex='4'> </fieldset> </form>"; } ?>