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="d开发者_Python百科escription" 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.
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']) == "") {
In your form put:
<form onsubmit = "return checkSubmit()">
Then in your Javascript somewhere:
function checkSubmit(){
if (form good)
return true;
else
return false;
}
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.
精彩评论