Welcome to WebmasterWorld Guest from 23.22.240.119

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php to have required fields on a form

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

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 0


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

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

Senior Member

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

joined:Feb 4, 2002
posts:5044
votes: 0


Here ya go...


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

Nick

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

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 0


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

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

Senior Member

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

joined:Feb 4, 2002
posts:5044
votes: 0


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

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

Senior Member

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

joined:Mar 15, 2002
posts:6807
votes: 0


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

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

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 0


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

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

Senior Member

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

joined:Feb 4, 2002
posts:5044
votes: 0


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

Nick

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

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2001
posts:1281
votes: 0


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.

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

Senior Member

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

joined:Feb 4, 2002
posts:5044
votes: 0


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

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members