PHP / HTML表单处理

该任务是创建一个HTML表单,用户输入所需的信息,并通过PHP处理表单数据以使用$ _post方法显示输出。 我似乎无法得到正确的输出,它只是显示我写的php代码。 任何洞察力都将不胜感激注:html代码冗长,但我确信它是正确的。 我的问题是PHP(下一个)代码。 以下是输出:

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"]; } } ?> 

第1页(HTML格式)

<!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代码:

<?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"];
 }

}
 ?>

你的代码中有一些错误。

  • <?之间有一个空格 标记和php在第1行。删除
  • if (count($_POST) > 0) {没有关闭花括号。 在结尾?>标记前添加一个大括号。
  • 你有电话的名称字段设置为'电话'。

    所以找到所有这些

     $_POST['telephone']
    

    并将其更改为

     $_POST['phone']
    
  • 另外,如果您想查看结果,请注释掉

      header('Location:HTMLForm.html');
    

  • PHP文件以.php扩展名保存并打开
  • PHP代码应位于页面顶部,位于任何HTML标记之前
  • 而不是使用$ variable ==''来检查它是否为空,使用名为empty($variable)函数,如果为空则返回true
  • 你在else语句的第一行做了一个重定向,所以其余的代码不会被执行,因此echo部分将永远不会被访问。 把这个header("Location:HTMLform.html"); 作为else范围的最后一个陈述

  • // 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';
        }
    

    另外,你从帖子中删减了什么?

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

    上一篇: PHP/HTML form processing

    下一篇: Parse error: syntax error, unexpected '[', expecting