Welcome to WebmasterWorld Guest from 54.144.79.200

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php to have required fields on a form

     

kapow

1:17 pm on Jun 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can someone tell me the php to have required fields on a form.

If fields a, b, c, not completed
display message'Please complete... Click your back button...'

Nick_W

3:02 pm on Jun 6, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Here ya go...


if(empty($field) {
Print($field." must be filled in please go back and do it again you twit!");
}

Nick

kapow

4:49 pm on Jun 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Nick_W

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'?

Nick_W

6:57 pm on Jun 6, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Heres something I think would be better:

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

DrDoc

1:02 am on Jun 7, 2002 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



As for your question whether print and echo is the same thing, technically - yes :)

I recommend you downloading the PHP manual.
[php.net...]

It is very useful ;)

kapow

10:59 am on Jun 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I finally got the following code to work, so I thought I would post it for anyone else.

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 -->

Nick_W

11:17 am on Jun 10, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



$error will just disapear when the script ends. No need to worry about it.

Nick

kapow

12:07 pm on Jun 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your help Nick_W
I'm thrilled that $error disapears when the script ends - in this case.

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.

Nick_W

12:35 pm on Jun 10, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If you want persistent variables just use sessions. They're very easy and there is an excellent section devoted to them in the manual

Nick