I've been trying to set up something which will check an HTML form, to make sure that the text fields have at least 1 character entered, before the form is submitted (to the next page).
The HTML form is on one page, and flows through to a 2nd page. The 2nd page then updates a text file. That all works fine as is, so I dont want to change it. But what I do need is the validator.
I've searched and read through numerous suggestions, have seen some suggestions for using ISSET, and done the script below.
but this doesnt work (because the form is always submitted / there are no error messages). I have tired numerous other suggestions (based on other tutorials I have read). But it seems, that regardless of whether the text fields are empty or not, the form will still always submit. (there is a timestamp on the next page, so I can tell what gets submitted.
Can someone tell me whether Isset will work in this case? If so, where I am going wrong. Or if not, what I can use to get a validator for the text fields. I'd rather use PHP for this, but if I need to use Javascript that's a possibility.
(Added: I think the problem is that even if the below $error messages occur, it will never show up, because the page is automatically redirected via the submit button. What I need it similar to a event handler - eg, if button submit is clicked, check for errors. If there are errors, print them. If not, submit form. this code doesnt do that).
Page 1 code.
<?php
if (isset($_POST['submit'])) {
$error = '';
if (trim($_POST['topic']) == '') {
$error = "Please enter a topic<br>");
}
if (trim($_POST['outline']) == '') {
$error = "Please enter an outline<br>";
}
if ($error == '') {
Echo "There are no errors";
} else {
echo "$error";
}
}
?>
<form style="" method="post" action="addtopic2.php">
Topic:<input name="topic" id="topicbox" maxlength="100" type="text">
Outline: <textarea input wrap="nowrap" rows="10" cols="120" name="outline">
</textarea><br><input name="submit" value="Submit" type="submit">
</form>
Page 2 code
<?php
$b = time ();
$c = date("g:i:s A D, F jS Y",$b);
print "Topic and Outline Submitted at $c";
$t = "Topic:";
$o = "Outline:";
$d = "Time Date:";
$topic = $_POST['topic'];
$outline = $_POST['outline'];
$data = stripslashes("$t $topic | $o $outline | $d $c |\n");
$fh = fopen("users.txt", "a");
fwrite($fh, $data);
fclose($fh);
?>