简体   繁体   English

PHP / HTML表单处理

[英]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. 任务是创建一个html表单,用户在其中输入所需的信息,并通过PHP处理表单数据以使用$ _post方法显示输出。 I can't seem to get the output right, it just basically displays the php code that I wrote. 我似乎无法正确输出,它只是显示我编写的php代码。 any insight is greatly appreciated Note: The html code is lengthy, but I'm sure it's correct. 注意:html代码很长,但是我确定它是正确的。 My problem is with the PHP(next) code. 我的问题是PHP(next)代码。 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) 第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 code: 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 file are saved with .php extension and has an opening of PHP文件以.php扩展名保存,并且以
  • PHP code should be at the top of the page, before any HTML tag PHP代码应该在页面顶部,在任何HTML标记之前
  • Instead of using $variable == '' to check if it's empty, use function called empty($variable) that return true if empty 而不是使用$ variable ==''来检查它是否为空,而使用称为empty($variable)函数,如果为空则返回true
  • 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. 您正在else语句的第一行中进行重定向,因此将不执行其余代码,因此将永远无法到达echo部分。 Put this header("Location:HTMLform.html"); 放置此header("Location:HTMLform.html"); as the last statement in the else scope 作为else范围中的最后一条语句

You have some errors in your code. 您的代码中有一些错误。

  • There is a space between <? <?之间有一个空格 tag and php in line 1. Remove that 第1行中的标记和php 。删除它
  • There is no closing curly brace for this , if (count($_POST) > 0) { . if (count($_POST) > 0) {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');

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? 另外,您从帖子中修剪什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM