homepage Welcome to WebmasterWorld Guest from 54.226.235.222
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php to have required fields on a form
kapow




msg:1268510
 1:17 pm on Jun 6, 2002 (gmt 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...'

 

Nick_W




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

Here ya go...


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

Nick

kapow




msg:1268512
 4:49 pm on Jun 6, 2002 (gmt 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'?

Nick_W




msg:1268513
 6:57 pm on Jun 6, 2002 (gmt 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

DrDoc




msg:1268514
 1:02 am on Jun 7, 2002 (gmt 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 ;)

kapow




msg:1268515
 10:59 am on Jun 10, 2002 (gmt 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 -->

Nick_W




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

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

Nick

kapow




msg:1268517
 12:07 pm on Jun 10, 2002 (gmt 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.

Nick_W




msg:1268518
 12:35 pm on Jun 10, 2002 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved