I am using a php contact form on my website.
I have a code to insert an X if field is empty when receiving the mail.
but I can’t accomplish this for < textarea >.
This is the code that works for me if name and telephone field is empty:
$empt = Array('name','telephone');
foreach($empt as $v){
if(empty($_POST[$v])) $_POST[$v] = 'X';
}
But I can’t accomplish this for the name of my < textarea >.
Does anyone know how to put an X if < textarea > field is empty? Many thanks!
2
Answers
There is an error in your PHP. You forgot to add the textarea to the php array. If your html textarea looks like this:
<textarea name="message"></textarea>
, then your PHP should include message in the foreach loop, and look like this:Sometimes if there are any whitespaces or new lines in your
textarea
, it won’t be considered empty. Consider trimming it’s value first:Although I don’t recommend it, if the PHP script is not correctly handling an empty textarea, you can adjust the value of the form’s inputs using JavaScript when the form is submitted.
For example: