tags:

views:

33

answers:

3

Hello! My script is supposed not to let the HTML form be submitted unless a user enters some data into a textarea control. However, the form gets submitted anyway and no message for the user is displayed. Could anyone see what I'm doing wrong?

Here is my HTML:

<textarea name="description" id="description" rows = "4" cols = "25">
    <?php echo $description?>
    </textarea>

As for PHP, neither this:

if ($_POST['description'] == "") {
            $errors .= 'Please enter a description of your invention.<br/><br/>';
        }

or this:

if (empty($_POST['description'])) {
            $errors .= 'Please enter a description of your invention.<br/><br/>';
        }   

work.

A: 

In your form put:

<form onsubmit = "return checkSubmit()">

Then in your Javascript somewhere:

function checkSubmit(){
  if (form good)
    return true;
  else
    return false;
}
robbrit
dangerous, never trust on client side validation alone!
joni
Where you replace 'form good' by suitable code, btw.
Colin Fine
+6  A: 

That's because the text area isn't empty! Note that you're putting a tab before <?php echo $description?>, which means at the very least the text area will always contain a tab.

Change your validation code to something like this:

if (trim($_POST['description']) == "") {
mellowsoon
Actually your text area probably contains a newline, a tab, and another new line.
mellowsoon
Yeah, either remove the whitespace completely, or trim it on the way into the database, otherwise every time you edit the description it's going to gain more and more leading/trailing whitespace. (Note also you need `htmlspecialchars()` over the `$description` to avoid HTML-injection leading to potential XSS security problems.)
bobince
@bobince Good call on mentioning the use of htmlspecialchars.
mellowsoon
This one worked! Thank you!
vlevsha
@bobince - Thank you htmlspecialchars().It's a very good point. I'm going to use it.
vlevsha
A: 

You have some white space between the <textarea> tags. Have you tried outputting $_POST['description'] to see if this is being posted back? The extra new lines may be messing it up. You can try adding a minimum length to it.

Drackir