PHP/HTML form processing

The assignment is to create an html form where the user enters the required information, and process the form data via PHP to display the output using $_post method. I can't seem to get the output right, it just basically displays the php code that I wrote. any insight is greatly appreciated Note: The html code is lengthy, but I'm sure it's correct. My problem is with the PHP(next) code. the following is the output:

0){ $Name = trim($_POST['name']); $adr = trim($_POST['address']); $City = trim($_POST['city']); $state = trim($_POST['state']); $zip = trim($_POST['zip']); $phone = trim($_POST['phone']); $email = trim($_POST['email']); $err = array(); if($Name == ''){ $err[] = "Please enter your name"; } if($adr == ''){ $err[] = "Please enter your address"; } if($City == ''){ $err[] = "Please enter your city"; } if($state == ''){ $err[] = "Please enter your State"; } if($zip == ''){ $err[] = "Please enter your zip"; } if($phone == ''){ $err[] = "Please enter your phone number"; } if($email == ''){ $err[] = "Please enter your email"; } if(count($err) > 0){ foreach($err as $value){ echo"$value
"; } echo " Go Back"; } else{ //header("Location:HTMLform.html"); echo "Name: " . $_POST["name"]; echo "Address: " . $_POST["address"] ; echo "City: " . $_POST["city"] ; echo "State: " . $_POST["state"]; echo "Zip: " . $_POST["zip"]; echo "Phone: " . $_POST["phone"]; echo "Email: " . $_POST["email"]; } } ?> 

page 1 (HTML FORM)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
  <title>Coffee Order</title>
 </head>
 <body><h1>The Coffee House</h1>


<div>
    <div><h3>Order Form</h3></div>
    <form name='frmInput' action="process.php" method="post">
    <table><tr><td>Coffee:</td>
        <td><select name="coffeeCode" id="Coffee">
<option value="">Select Coffee:</option><option value="bv">Boca Villa ($7.99/lb) 
</option>
<option value="sbr">South Beach Rhythm ($8.99/lb)</option>
<option value="pp">Pumpkin Paradise ($8.99/lb)</option>
<option value="ss">Sumatran Sunset ($9.99/lb)</option>
<option value="bb">Bali Batur ($10.95/lb)</option>
<option value="dd">Double Dark ($9.95/lb)</option></select></td></tr>
    <tr><td>
        Type:</td>
        <td>
<input type="radio" name="coffeeType" value="caf">Regular<br/>
<input type="radio" name="coffeeType" value="decaf">Decaffeinated
        </td>
    </tr>
    <tr>
        <td>Quantity (in pounds):</td>
        <td>
    <input type="text" name="quantity" maxlength="3" size="3" id="Quantity">
        </td>
    </tr>
    <tr>
        <td>Name:</td>
        <td>
            <input type="text" name="name" id="Name">
        </td>
    </tr>
    <tr>
        <td>E-mail address:</td>
        <td>
            <input type="text" name="email" id="Email">
        </td>
    </tr>
    <tr>
        <td>Telephone #:</td>
        <td>
    <input type="text" name="phone" maxlength="14" size="14" id="Telephone">
        </td>
    </tr>
    <tr>
        <td>Address:</td>
        <td>
            <input type="text" name="address" id="Address">
        </td>
    </tr>
    <tr>
        <td>City:</td>
        <td>
            <input type="text" name="city" id="City">
        </td>
    </tr>
    <tr>
        <td>State:</td>
        <td>
<input type="text" name="state" maxlength="2" size="2" 
style="text-transform: uppercase" id="State">
        </td>
    </tr>
    <tr>
         <td>Zip:</td>
        <td>
<input type="text" name="zip" maxlength="10" size="10" id="Zip">
         </td>
    </tr>
    <tr>
<td><input type="submit" value="Submit"></td><td><input    type="reset"></td>
    </tr>
    </table>
</form></div></body>
</html>

php code:

<?php
if (count($_POST) > 0){

$Name = trim($_POST['name']);
$adr = trim($_POST['address']);
$City = trim($_POST['city']);
$state = trim($_POST['state']);
$zip = trim($_POST['zip']);
$phone = trim($_POST['phone']);
$email = trim($_POST['email']);
$err = array();

if($Name == ''){
$err[] = "Please enter your name";
}
if($adr == ''){
$err[] = "Please enter your address";
}
if($City == ''){
$err[] = "Please enter your city";
}
if($state == ''){
$err[] = "Please enter your State";
}
if($zip == ''){
$err[] = "Please enter your zip";
}
if($phone == ''){
$err[] = "Please enter your phone number";
}
if($email == ''){
$err[] = "Please enter your email";
}

if(count($err) > 0){
   foreach($err as $value){
   echo"$value<br/>";
}
echo "<a href='HTMLform.html'> Go Back</a>";
}
 else{
//header("Location:HTMLform.html");
 echo "Name: " . $_POST["name"];

 echo "Address: " . $_POST["address"] ;

 echo "City: " . $_POST["city"] ;

 echo "State: " . $_POST["state"];

 echo "Zip: " . $_POST["zip"];

 echo "Phone: " . $_POST["phone"];

 echo "Email: " . $_POST["email"];
 }

}
 ?>

You have some errors in your code.

  • There is a space between <? tag and php in line 1. Remove that
  • There is no closing curly brace for this , if (count($_POST) > 0) { . Add a closing curly brace before the ending ?> tag.
  • You have the name field of telephone set to 'phone'.

    So find all lines having this

     $_POST['telephone']
    

    and change it to

     $_POST['phone']
    
  • Also, If you want to see the results, comment out

      header('Location:HTMLForm.html');
    

  • PHP file are saved with .php extension and has an opening of
  • PHP code should be at the top of the page, before any HTML tag
  • Instead of using $variable == '' to check if it's empty, use function called empty($variable) that return true if empty
  • You are doing a redirect in the first line of the else statement, so the rest of the code won't be executed, therefore the echo part will never be reached. Put this header("Location:HTMLform.html"); as the last statement in the else scope

  • old

    // is this suppose to be an array?
        $err = array();
        if($_POST['name'] == null){
            array_push($err,'error stuff stuff stuff');
        }
        print_r($err);
    
    // if not array
        $err = '';
        if($_POST['name'] == null){
            $err.= 'error stuff stuff stuff';
        }
    

    Also, What are you trimming from the post?

    链接地址: http://www.djcxy.com/p/69540.html

    上一篇: PHP邮件功能没有完成e的发送

    下一篇: PHP / HTML表单处理