如果内容是在表标签中组织的,如何使用表单标签?

时间:2014-04-17 18:29:23

标签: php html

我尝试使用表格并发现它很有趣,因为它使我更容易轻松地组织内容而不仅仅使用简单的div;但是,现在我的表单不会工作,我不确定是什么问题。

HTML:

<?php
        include('connect.php');
    ?>

    <div class="signUpWrapper">
    <h1 class="contentMainHeader" style="text-align: center"> Create a new Admin </h1>
    <form method="post" name="registry" action="main index.php?page=thankyouPage">
    <table class="signUpTable">
        <tr style="height: 5em">
            <td>
            <div>
            <h1 class="userHeader">Name</h1>
            </div>
            <div>
            <input type="text" name="firstName" class="textbox" placeholder="First Name"/>&nbsp&nbsp
            <input type="text" name="lastName" class="textbox" placeholder="Last Name"/>
            </div>
            </td>
        </tr>

        <tr style="height: 5em">
            <td>
            <div>
            <h1 class="userHeader">Email</h1>
            </div>
            <div>
            <input type="text" name="email" class="textbox" placeholder="Email">&nbsp&nbsp
            <input type="text" name="email2" class="textbox" placeholder="Re-Enter Email"/>
            </div>
            </td>
        </tr>

        <tr style="height: 2em;">
            <td>
            <div>
            <input type="text" name="altEmail" class="textbox" placeholder="Alternate Email">&nbsp&nbsp
            </div>
            </td>
        </tr>

        <tr style="height: 5em">
            <td>
            <div>
            <h1 class="userHeader">Password</h1>
            </div>
            <div>
            <input type="text" name="password" class="textbox" placeholder="Password"/>&nbsp&nbsp
            <input type="text" name="password2" class="textbox" placeholder="Re-Enter Password"/>
            </div>
            </td>
        </tr>

        <tr style="height: 5em">
            <td>
            <div>
            <h1 class="userHeader">Birthday</h1>
            </div>
            <div>
            <select name="month" class="dropDown">
            <option selected>Select Month</option>
            <option> January </option>
            <option> February </option>
            <option> March </option>
            <option> April </option>
            <option> May </option>
            <option> June </option>
            <option> July </option>
            <option> August </option>
            <option> September </option>
            <option> October </option>
            <option> November </option>
            <option> December </option>
            </select>
            <input type="text" name="day" class="dateTextBox" placeholder="Day (dd)"/>&nbsp&nbsp
            <input type="text" name="year" class="dateTextBox" placeholder="Year (yyyy)"/>
            </div>
            </td>
        </tr>

        <tr style="height: 5em">
            <td>
            <div>
            <h1 class="userHeader">Gender</h1>
            </div>
            <select name="gender" class="dropDown">
            <option selected>Select Gender</option>
            <option> Male </option>
            <option> Female </option>
            </select>
            </td>
        </tr>

        <tr style="height: 5em">
            <td>
            <div>
            <h1 class="userHeader">Phone Number</h1>
            </div>
            <div>
            <input type="text" name="phoneNumber" class="textbox" placeholder="Phone Number">&nbsp&nbsp
            </div>
            </td>
        </tr>
        <tr style="height: 8em"><td>
        <input type="submit" value="Register" class="css_button">
        </td>
        </tr>


    </table>
    </form>
    </div>

PHP:

<?php

    #error_reporting(0);    

    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $altEmail = $_POST['altEmail'];
    $gender = $_POST['gender'];
    $birthday = $_POST['month'] . "/" . $_POST['day'] . "/" . $_POST['year'];
    $phoneNumber = $_POST['phoneNumber'];
    $password = $_POST['password'];


    $sql = "INSERT INTO `admin` (`email`, `alternateEmail`, `firstName`, `lastName`, `password`, `DateOfBirth`, `Gender`, `phoneNumber`) VALUES ('$email', '$altEmail','$firstName', '$lastName', '$password', '$birthday', '$gender', '$phoneNumber')";
    $result = mysql_query($sql);
?>

它重定向到包含相同PHP代码的不同php文件,但它没有向phpmyadmin提交任何数据。

3 个答案:

答案 0 :(得分:0)

您忘记了操作标记来指定接收数据的目标php脚本

<form method="post" name="registry" action="yourpage.php">

答案 1 :(得分:0)

如果要将值提交到另一页面。您应该定义表单操作

<form method="post" name="registry" action="Filepath">

以下是 Form Action

的详细信息

如果您没有定义表单操作或将操作留空。表格提交到同一页面。

Example

答案 2 :(得分:0)

如果您的表单提交给自己,则不需要操作属性。但是,表单的select元素中缺少多个值。例如,

        <select name="month" class="dropDown">
        <option selected>Select Month</option>
        <option value="January"> January </option> <!--each option needs a value-->

所有选择选项必须具有值,否则它们在POST数组中的结果将为空。 我建议在提交按钮中添加一个名称:

<input type="submit" value="Register" name="Register" class="css_button" />

允许您检查表单是否已提交:

<?php

#error_reporting(0);    

if(isset($_POST['Register'])){

    $_POST = mysql_real_escape_string($_POST); //protect yourself from SQL injection...

    $country = $_POST['country'];
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    //etc...

最后,我认为INSERT失败是因为您命名的country列没有相应的$country变量来插入。这将为您留下9个已命名的列,并且只插入8个值:

 $sql = "INSERT INTO `admin` (`email`, `alternateEmail`, `firstName`, `lastName`, `password`, `DateOfBirth`, `country`, `Gender`, `phoneNumber`) VALUES ('$email', '$altEmail','$firstName', '$lastName', '$password', '$birthday', '$country', '$gender', '$phoneNumber')";
$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

我也没有在任何地方看到mysql_connect电话,所以我希望你能在某个地方连接数据库?