Hi,

Thanks for looking.

I have a contact form and everything else is checked against validation apart from the textarea. For some reason i cannot get textarea validation to work using php.

I have tried:

if (strlen($_POST['query'] == 0 ) ) {
	echo "<p>You have not entered anything in the text box provided. A minimum of 15 characters is required.</p>";
}

also tried it with < 15 and

if (empty($_POST['query'] ) ) {
	echo "<p>You have not entered anything in the text box provided. A minimum of 15 characters is required.</p>";
}

No matter how i try it won't validate or give the above message. Basically meaning users can submit without the need of entering anything in the textarea.

If i put this:

if ($_POST['query'] < 15 ) {
	echo "<p>You have not entered anything in the text box provided. A minimum of 15 characters is required.</p>";
}

Problem i have then is it will display the error and won't submit form which is great , but even thou i type over 15 characters it gives me the error still and wont submit.

I am now at the end of my teather. I don't know why and is there another way to validate a text area?

I am boggeled by this.

Any help much appreciated as always.

Thank you,
Genieuk

A) Post your form
B) if (strlen($_POST['query'] == 0 ) ) { should be if (strlen($_POST['query']) == 0) {

A) Post your form
B) if (strlen($_POST['query'] == 0 ) ) { should be if (strlen($_POST['query']) == 0) {

Thank you for a prompt reply. Here is my form. As i got it now. It will validate textarea but if i enter more than 15 characters it still gives me error that i not filled it in.

<?php include ("top.inc"); ?>
<script type= "text/javascript">
var RecaptchaOptions = {
theme: 'white'
};
</script>
<h1>Contact us</h1>
<h6>* Required Fields</h6>
<?php 
if (isset($_POST['submitted'])) { // Handle the form.

// Avoid hacking
$name = htmlspecialchars( stripslashes( strip_tags ($_POST['name'] ) ) );
$email = htmlspecialchars( stripslashes( strip_tags($_POST['email'] ) ) );
$query = htmlspecialchars( stripslashes( strip_tags( nl2br($_POST['query'] ) ) ) ); 

// Validate form fields
if ( empty($_POST['name'] ) ) {
	echo "<p>Your name is required in order to submit this form.</p>";
	$err++;
}

if ( empty($_POST['email'] ) ) {
	echo "<p>Your email address is required in order to submit this form.</p>";
	$err++;
}

if ($_POST['query'] < 15 ) {
	echo "<p>You have not entered anything in the text box provided. A minimum of 15 characters is required.</p>";
	$err++;
}

require_once('recaptchalib.php');
$privatekey = "REMOVED FOR SECURITY REASONS";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if(!$resp->is_valid) {
echo "The Captcha Code you entered is incorrecy. Please try again.";
$err++;
}

if ($err == 0) {

	
	// Details to be sent with email
$to = "admin@mydomain.com";
$subject = "Email From My Site";
$message = "$query";
$headers = "From: $email";


// Mail the user
mail( $to, $subject, $message, $headers );
// Display message
echo "<p>Thank you <b>$name</b> Your enquiry has been successfully sent to us. If you required a responce we shall reply within the next seven working days. We will reply to you at the email address you had given: <b>$email</b>.</p>";
}
}

?>

<form action="contactus.php" method="post" enctype="multipart/form-data">

<label for="name"><p>* Name:</p></label> <input type="text" id="name" name="name" maxlength="90" value="<?php if (isset($_POST["name"])) { echo htmlspecialchars($_POST["name"]); } ?>"  />

<label for="email"><p>* Email Address:</p></label> <input type="text" id="email" name="email" maxlength="50" value="<?php if (isset($_POST["email"])) { echo htmlspecialchars($_POST["email"]); } ?>"  />

<label for="query"><p>* Query:</p></label>

<textarea cols="60" rows="12" id="query" name="query" value="<?php if (isset($_POST["query"])) { echo htmlspecialchars($_POST["query"]); } ?>"> </textarea><br /><br />

<?php // Display captcha image
require_once('recaptchalib.php');
$publickey = "REMOVED FOR SECURITY REASONS"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<br />
<input type="submit" name="submit" id="submit" value="Send Form"  />
<input type="reset" name="reset" id="reset" value="Reset Form" />

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php include ("footer.inc"); ?>

Thank you,
Genieuk

if ($_POST['query'] < 15 ) {
	echo "<p>You have not entered anything in the text box provided. A minimum of 15 characters is required.</p>";
	$err++;
}

That doesn't do what you think it does. It tries to turn the string into a number and compare it.
You want to do this

if (strlen($_POST['query']) < 15 ) {

lol, i am so sorry, sometimes i am on another planet.

Thank you all sorted now.

Maybe you can help with one thing thou?

if a user enters data in textarea and a field was validated incorrect it does not remember what they typed in the query box. I have done this:

<textarea cols="60" rows="12" id="query" name="query" value="<?php if (isset($_POST["query"])) { echo htmlspecialchars($_POST["query"]); } ?>"> </textarea>

Problem is it works great at remembering the name and email field but not the query field. As value i used the php code above like for name and email but no luck. Any suggestions to why?

I dont know why it be different to a text box.

Thank you,
genieuk

Textareas work by having their value as the content between the tags

<textarea cols="60" rows="12" id="query" name="query">
<?php if (isset($_POST["query"])) { echo htmlspecialchars($_POST["query"]); } ?>
</textarea>

Thank you so much for all your help. Much appreciated.

Learnt something else today :)

All sorted.

Regards,
Genieuk

This question has already been answered. Start a new discussion instead.