Teach Yourself PHP in 10 Minutes [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Teach Yourself PHP in 10 Minutes [Electronic resources] - نسخه متنی

Chris Newman

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Displaying Validation Warnings


Another issue to consider is where to send the user when validation fails. So far we have assumed that a form submits to a processing script, and when one or more validation errors are found, the form prompts the user to use his or her browser's Back button to fix the errors.Lesson 14, "Cookies and Sessions."

A good technique is to have the form and processing script in the same file and have the form submit to itself. This way, if there are errors, they can be displayed on the same page as the form itself, and the previously entered values can be automatically defaulted into the form.

Listing 13.1 shows a fairly complete example of a registration form, register.php. The name and email address fields are required, but the telephone number is optional.

Listing 13.1. A Sample Registration Form with Required Fields


<?php
$required = array("name" => "Your Name",
"email" => "Email Address");
foreach($required as $field => $label) {
if (!$_POST[$field]) {
$err .= "$label is a required field <br>";
}
}
if ($err) {
echo $err;
?>
<FORM ACTION="register.php" METHOD=POST>
<TABLE BORDER=0>
<TR>
<TD>Your Name</TD>
<TD><INPUT TYPE=TEXT SIZE=30 NAME="name"
VALUE="<?php echo $_POST["name"];?>"></TD>
</TR>
<TR>
<TD>Email Address</TD>
<TD><INPUT TYPE=TEXT SIZE=30 NAME="email"
VALUE="<?php echo $_POST["email"];?>"></TD>
</TR>
<TR>
<TD>Telephone</TD>
<TD><INPUT TYPE=TEXT SIZE=12 NAME="telephone"
VALUE="<?php echo $_POST["telephone"];?>"></TD>
</TR>
</TABLE>
<INPUT TYPE=SUBMIT VALUE="Register">
</FORM>
<?php
}
else {
echo "Thank you for registering";
}
?>

Note that the warning messages in this example appears even if the form has not yet been submitted. This could be improved by also checking for the existence of the $_POST array in the script by using is_array, but the check for $err would also need to look for $_POST; otherwise, the form would never be displayed.Lesson 19, "Using a MySQL Database." Alternatively, the script could force the browser to redirect the user to another page automatically by using a Location HTTP header, as follows:


header("Location: newpage.php");


/ 126