Forum Moderators: coopster
If I'm reading it right then that will work for one field. What does it need for multiple fields? - so the message says
A, B and F, must be filled in please go back and do it again you twit!
How about building a message variable?:
if(empty($field_A ¦¦ $field_B $field_C $field_D $field_E) {
$message = "You twit, Hit the back buttion and compete the following on your form: ";
if(empty($field_A) {$message .= " A"}
if(empty($field_B) {$message .= " B"}
etc...
}
else {$message = "Thankyou"}
Print($message)
Would that work?
I'm still new to php. Is 'Print' the same as 'Echo'?
say you have 3 fields:
/* names of the fields as keys with there values as the array elements */
$fields=array('name' => $name, 'email' => $email, 'pass' => $pass);foreach($fields as $key => $val) {
if(empty($val)) { // checks value of each field
$error_msg.="$key is empty<br />";
}
}
if($error_msg=='') {
/* all is well so do what you need to */
}
It's completely untested but the principle is sound
Nick
I recommend you downloading the PHP manual.
[php.net...]
It is very useful ;)
Can anyone explain to me how $error gets purged when the fileds are completed? - I thought php4 variables kept their content i.e. $error = "You must complete all required fields".
<?
if ($submit) {
// Required fields in the line below
if (!$note ¦¦ !$email ¦¦ !$address)
{ $error = "You must complete all required fields"; }
if (!$error) {
// Code to do something with the data here
$mailed = "Your request has been sent. Thankyou";
}}?>
<!-- html for page layout and stuff -->
<? echo $mailed; ?>
<? if ($error ¦¦ !$submit) { ?>
<h3><?php echo $error; ?></h3>
<!-- html for the form -->
<form method="POST" action="<?php echo $PHP_SELF ?>">
<!-- html input boxes look like this: -->
<input type="text" name="name" size="30" value="<? echo $name ?>">
<!-- end of form -->
<? } ?>
<!-- html for remaining page stuff -->
When I first read a php book (or tried to), it was php3 - which said a variable's content must be passed from form to form (if there are multiple stages) by using hidden input fields. I then heard php4 doesn't require this. So I was surprised to find $error loses its content.
I'm still a php beginner, I need to get my knowledge straight so that I can do this kind of thing.