无法使用php _POST方法从我的表单中获取数据

时间:2014-03-05 21:08:56

标签: php html post

我有PHP代码的问题我试图从我的HTML表单读取数据,我一直得到null 这是我的PHP代码

<html>
<body>
    <?php
        $dbhost="127.0.0.1";
        $dbuser="root";
        $dbpass="";
        $dbname="mss";
        $connection=mysql_connect($dbhost,$dbuser,$dbpass,$dbname);
        if(mysql_errno()) {
            die("Database Connection failed:" . mysql_error() . "(" . mysql_errno() .")");      
        }
   ?>
    first Name : <?php if($_SERVER['REQUEST_METHOD'] == "POST"){ $first = $_REQUEST['firstName']; echo $first; } ?><br>
    last Name : <?php $last = (isset($_REQUEST['lastName']) ? $_REQUEST['lasttName']: "hello"); echo $last; ?><br>
    email : <?php $email = (isset($_REQUEST['email']) ? $_REQUEST['email']: "hello"); echo $email; ?><br>
    street address : <?php $streetAddress = (isset($_REQUEST['streetAddress']) ? $_REQUEST['streetAddress']: "hello"); echo $streetAddress; ?><br>
    city : <?php $city = (isset($_REQUEST['city']) ? $_REQUEST['city']: "hello"); echo $city; ?><br>
    state : <?php $state = (isset($_POST['state']) ? $_POST['state']: "hello"); echo $state; ?><br>
    Zip code : <?php $zipCode = (isset($_POST['zipCode']) ? $_POST['zipCode']: "hello"); echo $zipCode; ?><br>
    college : <?php $college = (isset($_POST['college']) ? $_POST['college']: "hello"); echo $college; ?><br>
    userName : <?php $username = (isset($_POST['username']) ? $_POST['username']: "hello"); echo $username; ?><br>
    password : <?php   $password = (isset($_POST['password']) ? $_POST['password']: "hello"); echo $password; ?><br>
</body>

以下是我的表格

的html代码
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Add New Employee</title>
    <script type="text/javascript" src="new employee validate.js"></script>
</head>
<body>
    <h1 style="font-family:Verdana, Geneva, sans-serif; font-size:24px;" align="center">Add New Employee</h1>
    <form name="AddNewEmployee" onSubmit="return validation()" action="AddEmployeePhp.php" method= "POST">
        <table align="center">
            <tr>
                <td><font color="#FF0000">*</font>   First Name:</td>
                <td><input type="text" id="firstName" name ="firstName" required = "required"/></td>
            </tr>
            <tr>
                <td><font color="#FF0000">*</font>Last Name:</td>
                <td><input type="text" id="lastName" name ="lastName" required = "required"/></td>
            </tr>
            <tr>
                <td><font color="#FF0000">*</font>E-mail Address:</td>
                <td><input type="text" id="email" name="email" required = "required"/></td>
            </tr>
            <tr>
                <td><font color="#FF0000">*</font>Cell Phone Number:</td>
                <td><input type="text" id="phone" name="phone" required = "required"/></td>
            </tr>
            <tr>
                <td>Street Address:</td>
                <td><input type="text" id="streetAddress" name= "streetAddress"/></td>
            </tr>
            <tr>
                <td>City:</td>
                <td><input type="text" id="city" name = "city"/></td>
            </tr>
            <tr>
                <td>State:</td>
                <td><input type="text" id="state" name ="state"/></td>
            </tr>
            <tr>
                <td>Zip Code:</td>
                <td><input type="text" id="zipCode" name="zipCode"/></td>
            </tr>
            <tr>
                <td>College:</td>
                <td><input type="text" id="college" name="college"/></td>
            </tr>
            <tr>
                <td><font color="#FF0000">*</font>User Name:</td>
                <td><input type="text" id="username" name = "username" required = "required"/></td>
            </tr>
            <tr>
                <td><font color="#FF0000">*</font>Password:</td>
                <td><input type="password" id="password" name = "password" required = "required"/>    </td>
            </tr>
            <tr>
                <td><font color="#FF0000">*</font>Confirm Password:</td>
                <td><input type="password" id="confirmPassword" required = "required"/></td>
            </tr>
            <tr>
                <td>
                    <p>&nbsp;</p>
                </td>
                <td>
                    <p>&nbsp;</p>
                </td>
            </tr>
            <tr>
                <td><input type="submit" name="Create Employee" value="Create Employee"/></td>
                <td><input type="button" name="Clear All" value="Clear All" /></td>
            </tr>
        </table>
    </form>
    &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    <label> Note: All <font color="#FF0000">*</font> Fields are required please fill them all</label>
</body>

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这个拼写错误$_REQUEST['lasttName']应为$_REQUEST['lastName']

另外,这个<input type="password" id="confirmPassword" required = "required"/>没有其他参考,所以我不确定你为什么要使用/包含它。

我也没有在validation()中看到onSubmit="return validation()"函数的任何引用,所以如果你没有它的功能(我在你发布的代码中没有看到),那么你需要删除它。

我注意到的另一件事是有一些包含空格的命名表单元素;那是无效的。

I.e。:如果您决定稍后在PHP中使用它们,则name="Create Employee"使用下划线name="Create_Employee"name="Clear All" name="Clear_All"