Adrian56 2 Posted June 16, 2013 Report Share Posted June 16, 2013 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>"; } ?> Quote Link to post Share on other sites
HartleySan 826 Posted June 16, 2013 Report Share Posted June 16, 2013 There is likely some whitespace between the textarea start tag and the textarea end tag. That will be interpreted by the browser as whitespace that you actually want to enter within the textarea. Try putting your textarea tags together as follows: <textarea name='message' rows='7' cols='40' tabindex='3'></textarea><br> 1 Quote Link to post Share on other sites
Adrian56 2 Posted June 16, 2013 Author Report Share Posted June 16, 2013 Absolutely spot on!! I'd added spaces/breaks at that point in the coding for readability. Works perfectly now, thank you. Now I can get on with making the form sticky. Quote Link to post Share on other sites
HartleySan 826 Posted June 16, 2013 Report Share Posted June 16, 2013 Yeah, one of the tricky things about HTML is knowing about those edge cases when whitespace actually matters. Anyway, glad you got what you wanted. Quote Link to post Share on other sites
Adrian56 2 Posted June 16, 2013 Author Report Share Posted June 16, 2013 Okay all that you have said in this and 'Trying To Return To Previous Page From A Form' has worked perfectlybut... below is the source code from the formpage being 'posted' to itself. Every thing looks as though it should be fine but the value of the message field 'dddd' is not displayed. Have I hit the edge again - I'm really not doing very well with this message field! <form method='post' action='formpage.php?urlinform=http://localhost:8888/Any_root/anotherpage.html&email=&subject=sssss&message=dddd'> <fieldset><legend>Your Contact Information</legend> Email: <input name='email' type='text' value='' tabindex='1'><br> Subject: <input name='subject' type='text' value='sssss' tabindex='2'><br> Message:<br> <textarea name='message' rows='7' cols='40' value='dddd' tabindex='3'></textarea><br> <input type='hidden' name='urlinform' value='http://localhost:8888/Any_root/anotherpage.html'> <input type='submit' id='submit' tabindex='4'> </fieldset> </form> Quote Link to post Share on other sites
HartleySan 826 Posted June 16, 2013 Report Share Posted June 16, 2013 That's because text area values are obtained by the text between the <textarea> and </textarea> tags, not by the value set for the value attribute. In fact, by definition. textarea elements do not have a value attribute. (Source: http://www.w3schools.com/tags/tag_textarea.asp) 1 Quote Link to post Share on other sites
Adrian56 2 Posted June 17, 2013 Author Report Share Posted June 17, 2013 Once again I am in your debt. Many thanks Quote Link to post Share on other sites
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.
Note: Your post will require moderator approval before it will be visible.