Page 1 of 1

Required fields in PHP form

Posted: Wed Nov 26, 2003 10:46 am
by sch0ol_0f_hard_kn0cks
Hello, first post here :?

Also a newbie. I'm moving my site from my free server because they don't accept commercial websites. Prob is now I gotta do all the forms myself. I have the php form2mail figured out pretty much, now I just need to get it to check for required fields...anyone know just how to do that?

Heres the form:

<form method="post" action="form2mail.php" enctype="application/x-www-form-urlencoded">

<input type="hidden" name="_From" value="sch0ol_0f_hard_kn0cks@yahoo.com">
<input type="hidden" name="_To" value="sch0ol_0f_hard_kn0cks@yahoo.com">
<input type="hidden" name="_Subject" value="Ad Placement">
<input type="hidden" name="_Location" value="form.php">
<table cellpadding="4" width="533" cellspacing="0" border="0">
<tr>
<td valign="TOP" width="40%"><font><p>Name: </p></font></td>
<td valign="TOP" width="60%"><input type="text" name="name" maxlength="20" width="81" /></td>
<td valign="TOP" width="40%"><font><p>Last Name: </p></font></td>
<td valign="TOP" width="60%"><input type="text" name="lname" maxlength="20" width="81" /></td>
</tr>
<tr>
<td><font><p>Address: </p></font></td>
<td><input type="text" name="address" maxlength="20" width="81" /></td>
<td><font><p>City: </p></font></td>
<td><input type="text" name="city" maxlength="20" width="81" /></td>
</tr>
<tr>
<td><font><p>Postcode: </p></font></td>
<td><input type="text" name="post_code" size="5" maxlength="5" width="81" /></td>
<td><font><p>Phone: </p></font></td>
<td><input type="text" name="phone" maxlength="20" width="81" /></td>
</tr>
<tr>
<td><font><p>E-Mail: </p></font></td>
<td><input type="text" name="email" size="20" maxlength="50" width="881" /></td>
</tr>
<tr>
<td><font><p>Contact per:</p></font></td>
<td><font><input type="radio" name="contact" value="E-Mail" checked />e-mail
<input type="radio" name="contact" value="Phone" />phone
<input type="radio" name="contact" value="Post" />post</font>
</td>
</tr>
<tr>
<td></td>
<td><p><input type="reset" name="reset" value="Reset" /></p></td>
<td><p><input type="submit" name="submit" value="Send" /></p></td>
</tr>
</table>
</form>

Heres the code:

<?php
$message="The form ".$_POST{"_Location"}." was filled in on ".date("m-d-Y H:i:s (D) (O)")."<HR>";
foreach ($_POST as $key => $value) {
if($key{0} != "_") $message .= "<B>$key</B>: ".nl2br(htmlspecialchars($value))."<BR>";
}
mail($_POST{"_To"}, $_POST{"_Subject"}, $message, "From: ".$_POST{"_From"}."\nContent-type: text/html\n");
?>

Now how can I get it to check for required fields and bring the visitor back to the form page to fill them in if blank?[/php_man]

Posted: Wed Nov 26, 2003 10:50 am
by JayBird
Welcome to the forum.

Read the forum description before you post, cos i think you'll find you posted in the wrong place.

Anyway...

There are two approaches you could take.

You could use Javascript to check the rerquired fields. Advantages of this is that you don't actually have to submit the form to check the field, they are check before submission.

The checking can be done using PHP, but the form has to be submitted before the checking can occur. Also, not everyone has Javascript enabled.

What kind of checking do you wanna do? Do you just wanna check the something has been entered, or a particular thing, like an email or a date?

Mark

Posted: Wed Nov 26, 2003 11:06 am
by Weirdan
Bech100 wrote: There are two approaches you could take.
The best one is probably the both ;). JS validation for customer (so he knows the form is filled properly even before it's submitted) and server-side validation to ensure that customer do not trying to break you webapp (And for those who have js disabled).

Posted: Wed Nov 26, 2003 11:08 am
by JayBird
true, true....your always oe step ahead :)

Mark

Posted: Wed Nov 26, 2003 3:51 pm
by sch0ol_0f_hard_kn0cks
Hmm. I could use javascript in this case because on a percentage basis most people have it enabled don't they? Anyone want to paste a simple javascript checker and let me know where to put it :D ? This isn't rocket science here I'm just using it for the forms that aren't paid for (subscription, free personals etc.) The ones I actually will be charging for get processed by a third party credit card server.

ps if someone just checked the personals page it's still the one on the free server which comes with forms included.

Posted: Wed Nov 26, 2003 6:57 pm
by mchaggis
I would still use both methods, if you are submitting the form to it'self you can simply use a php array to form a list of the required fields and then use this array both to generate the js for you ie:

Code: Select all

$required_fields= Array(
    'name',
    'lname',
    'post_code'
);
Then in your form:

Code: Select all

<script>
    function check_required_fields() {
<?php for($x=0; $x < count($required_fields}; $x++) { ?>

        if (form[0].<?=$required_fields[$x]?>.value = '') {
             alert('Field <?=$required_fields[$x]?> must be specified!");
             return false;
        }

<? } ?>
        return true;
    }
</script>

<!-- SNIP -->

<form method="post" action="form2mail.php" enctype="application/x-www-form-urlencoded" OnSubmit="return check_required_fields()">

You can then do the same in your form2mail.php script:

Code: Select all

$error_count = 0;
for($x=0; $x < count($required_fields}; $x++) { 

    if ($HTTP_POST_VARS[$required_fields[$x]] == '') {
        print "Field: $required_fields[$x] must be specified!<br />";
        $error_count=0;
    }

}

if ($error_count == 0) {
    // Process data
} else {
    // Redisplay the form
}
Hope tis helps

Posted: Wed Nov 26, 2003 8:42 pm
by sch0ol_0f_hard_kn0cks
This place ROCKS! Thanks mchaggis and EVERYONE for your feedback I hope I can someday soon be of help to some other newbie! Hopefully my newbie self can figure out how to insert this into my current code :-)

Posted: Wed Nov 26, 2003 9:31 pm
by sch0ol_0f_hard_kn0cks
Ok...I tried really hard to figure out what is wrong here but I'm getting this message:

Parse error: parse error, expecting `')'' in /var/www/html/subscribe2mail.php on line 10

I changed the name of the script because I'll have a couple of different forms, each with different required fields.

Heres what I've pieced together so far...

<?php
$required_fields= Array(
    'fname',
    'lname',
    'mail'
);
$error_count = 0;
for($x=0; $x < count($required_fields}; $x++) {
    if ($HTTP_POST_VARS[$required_fields[$x]] == '') {
        print "Field: $required_fields[$x] must be specified!<br />";
        $error_count=0;
    }
}
if ($error_count == 0) {
    // Process data
} else {
    // Redisplay the form
}
$message="The form ".$_POST{"_Location"}." was filled in on ".date("m-d-Y H:i:s (D) (O)")."<HR>";
foreach ($_POST as $key => $value) {
if($key{0} != "_") $message .= "<B>$key</B>: ".nl2br(htmlspecialchars($value))."<BR>";
}
mail($_POST{"_To"}, $_POST{"_Subject"}, $message, "From: ".$_POST{"_From"}."\nContent-type: text/html\n");
?>

Posted: Wed Nov 26, 2003 9:44 pm
by sch0ol_0f_hard_kn0cks
as soon as I posted that I saw an error in this line and fixed it:

for($x=0; $x < count($required_fields}; $x++) {

for($x=0; $x < count($required_fields); $x++) {

However I'm still getting that same error

Posted: Thu Nov 27, 2003 2:19 am
by twigletmac
That's weird, I copied and pasted your code, fixed the error you'd noticed and didn't get any further parse errors?

Mac

Posted: Thu Nov 27, 2003 11:03 pm
by sch0ol_0f_hard_kn0cks
Yes, I just copied ONLY the php code (no html) and saved it as "subscribe2mail.php" and then a really strange thing happened...now I'm getting missing ")" parse error on line 3???

<?php
$required_fields= Array(
    'fname', // <----this is line three right???
    'lname',
    'mail'
);
$error_count = 0;
for($x=0; $x < count($required_fields); $x++) {
    if ($HTTP_POST_VARS[$required_fields[$x]] == '') {
        print "Field: $required_fields[$x] must be specified!<br />";
        $error_count=0;
    }
}
if ($error_count == 0) {
    // Process data
} else {
    // Redisplay the form
}
$message="The form ".$_POST{"_Location"}." was filled in on ".date("m-d-Y H:i:s (D) (O)")."<HR>";
foreach ($_POST as $key => $value) {
if($key{0} != "_") $message .= "<B>$key</B>: ".nl2br(htmlspecialchars($value))."<BR>";
}
mail($_POST{"_To"}, $_POST{"_Subject"}, $message, "From: ".$_POST{"_From"}."\nContent-type: text/html\n");
?>

This is really freaky, could if have something to do with my Mac? I just downloaded the latest php. Btw when I try it on my mac I get nothing...just a blank page, then when I try it from my web server I get that error message.

Btw HAPPY TURKEY DAY ALL!

p.s. I've been spicing up a Guestbook Program I found on here and will be posting it when I'm done in appreciation :-)