1

我想将信息从 HTML 表单传递到 PHP 页面。我的问题是表单中的数据没有被提交。我究竟做错了什么 ?我在 xxamp 上使用 phpmyadmin。我知道这是一个很长的代码,但只需花几秒钟时间尝试弄清楚。我真的需要这个...谢谢

HTML 表格

PHP代码:

if($_POST['FormSubmit'] == "Submit") 
{
    $errorMessage = "Connection cannot be established with the database!";

    if(empty($_POST['firstname'])) 
    {
        $errorMessage .= "<li>You forgot to enter your first name!</li>";
    }
    if(empty($_POST['surname'])) 
    {
        $errorMessage .= "<li>You forgot to enter your surname!</li>";
    }
    if(empty($_POST['email'])) 
    {
        $errorMessage .= "<li>You forgot to select your email!</li>";
    }


    $varDate = $_POST['preferred_date'];
    $varTime = $_POST['preferred_time'];
    $varAdult = $_POST['adult'];
    $varType = $_POST['excursion_type'];
    $varInfo = $_POST['add_info'];
    $varChildren = $_POST['children'];
    $varFname = $_POST['first_name'];
    $varSurname = $_POST['surname'];
    $varEmail = $_POST['email'];
    $varPhone = $_POST['telephone'];

    if(empty($errorMessage)) 
    {
        $db = mysql_connect("localhost","root","password");
        if(!$db) die("Error connecting to MySQL database.");
        mysql_select_db("reservation" ,$db);

        $sql = "INSERT INTO online_reservation (preferred_date, preferred_time, adults, children, type, additional_info, first_name, surname, email, telephone) VALUES (".
                        PrepSQL($varDate) . ", " .
                        PrepSQL($varTime) . ", " .
                        PrepSQL($varAdult) . ", " .
                        PrepSQL($varChildren) . ", " .
                        PrepSQL($varType) . ", " .
                        PrepSQL($varInfo) . ", " .                          
                        PrepSQL($varFname) . ", " .
                        PrepSQL($varSurname) . ", " .
                        PrepSQL($varEmail) . ", " .
                        PrepSQL($varPhone) . ")";
        mysql_query($sql);

        echo("Request Submitted!");
        exit();
    }
}
    // function: PrepSQL()
// use stripslashes and mysql_real_escape_string PHP functions
// to sanitize a string for use in an SQL query
//
// also puts single quotes around the string
//
function PrepSQL($value)
{
    // Stripslashes
    if(get_magic_quotes_gpc()) 
    {
        $value = stripslashes($value);
    }

    // Quote
    $value = "'" . mysql_real_escape_string($value) . "'";

    return($value);
}
?>

HTML表格:

  <form name="reservation" method="post" onsubmit="return formValidator()" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
                <input name="service_provider" id="service_provider" value="Casela Nature &amp; Leisure Park (Tours and Excursions)" type="hidden" />
                <table style="" align="center" border="0" cellpadding="0" cellspacing="0" width="630">
                    <tbody>
                        <tr>
                            <td colspan="7"><div style="height: 10px;"></div></td>
                        </tr>
                        <tr valign="top">
                            <th align="right" height="25" valign="top" width="150">Preferred date(MM/DD/YY)*</th>
                            <td align="center" height="25" valign="top" width="10">:</td>
                            <td align="left" height="25" valign="top" width="150">
                            <input name="preferred_date" id="preferred_date" style="width: 75px;" onblur="isValidDate(); checkDate();" type="text" value="<?=$varDate;?>" />
                            </td>
                            <td height="25" width="10"><div style="height: 25px; width: 25px;"></div></td>
                            <th align="right" height="25" valign="top" width="150">Preferred time</th>
                            <td align="center" height="25" valign="top" width="10">:</td>
                            <td align="left" height="25" valign="top" width="150">
                            <input name="preferred_time" id="preferred_time" value="<?=$varTime;?>" style="width: 50px;" onblur="validatetime(this.value,'Preferred Time');" type="text" />
                            <div></div></td>
                        </tr>
                        <tr valign="top">
                            <td colspan="7"></td>
                        </tr>
                        <tr valign="top">
                            <th colspan="3" align="right">Number
                            of adults (aged above 12 yrs) *</th>
                            <td align="center" height="25" width="10">:</td>
                            <td colspan="3" align="left">
                            <input name="adult" id="adult" value="<?=$varAdult;?>" style="width: 30px;" type="text" />
                            </td>
                        </tr>
                        <tr valign="top">
                            <th colspan="3" align="right">Number
                            of children (aged 3 to 12 yrs) *</th>
                            <td align="center" height="25" width="10">:</td>
                            <td colspan="3" align="left">
                            <input name="children" id="children" value="<?=$varChildren;?>" style="width: 30px;" type="text" />
                            </td>
                        </tr>
                        <tr valign="top">
                            <th colspan="3" align="right">Please
                            specify type of excursion</th>
                            <td align="center" height="25" width="10">:</td>
                            <td colspan="3" align="left">
                            <input name="excursion_type" id="excursion_type" value="<?=$varType;?>" style="width: 300px;" type="text" />
                            </td>
                        </tr>
                        <tr valign="top">
                            <th colspan="7" align="left">Additional
                            information and special requirements if any</th>
                        </tr>
                        <tr valign="top">
                            <td colspan="7"></td>
                        </tr>
                        <tr>
                            <td colspan="6"><textarea style="width: 490px; height: 100px;" id="add_info" name="add_info" type="<?=$varInfo;?>"></textarea></td>
                        </tr>
                        <tr valign="top">
                            <td colspan="7"><div style="height: 5px;"></div></td>
                        </tr>
                        <tr valign="top">
                            <td colspan="7">
                            <div style="text-align: justify;">
                                <b>Your
                                personal information</b>
                                <br />
                                Please note that filling the correct information below is essential.
                                Tropicscope.com declares that personal information collected below will
                                not be diffused, sold or re-used by the website.
                            </div></td>
                        </tr>
                        <tr valign="top">
                            <td colspan="7"></td>
                        </tr>
                        <tr valign="top">
                            <th colspan="3" align="right">First
                            name* </th>
                            <td align="center" height="25" width="10">:</td>
                            <td colspan="3" align="left">
                            <input name="first_name" id="firstname" value="<?=$varFname;?>" style="width: 300px;" onblur="validateName(this.value,'Firstname'); " type="text" />
                            </td>
                        </tr>
                        <tr valign="top">
                            <th colspan="3" align="right">Surname
                            *</th>
                            <td align="center" height="25" width="10">:</td>
                            <td colspan="3" align="left">
                            <input name="surname" id="surname" value="<?=$varSurname;?>" style="width: 150px;" onblur="validateName(this.value,'Surname');" type="text" />
                            </td>
                        </tr>
                        <tr valign="top">
                            <th colspan="3" align="right">E-mail
                            address *</th>
                            <td align="center" height="25" width="10">:</td>
                            <td colspan="3" align="left">
                            <input name="email" id="email" value="<?=$varEmail;?>" style="width: 250px;" onblur=" validateEmail(); validateBlank(this.value,'Email address');" type="text" />
                            </td>
                        </tr>
                        <tr valign="top">
                            <th colspan="3" align="right">Telephone*</th>
                            <td align="center" height="25" width="10">:</td>
                            <td colspan="3" align="left">
                            <input name="telephone" id="telephone" value="<?=$varPhone;?>" style="width: 100px;" onblur="validatephone(this.value,'Telephone number');" type="text" />
                            </td>
                        </tr>
                        <tr valign="top">
                            <th colspan="3" align="right"></th>
                            <td align="center" height="25" width="10">
                            <br />
                            </td>
                            <td colspan="3" align="left">
                            <br />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3"></td>
                        </tr>
                        <tr valign="top">
                            <td colspan="7" align="center">
                            <div style="height: 30px;">
                                <input style="height: 25px;" value="Submit" onclick="validateSubmit();" type="submit" name="FormSubmit" />
                            </div></td>
                        </tr>
                    </tbody>
                </table>
            </form>
4

1 回答 1

5
 if(empty($errorMessage)) 

总是是假的,因为你在$errorMessage这里设置:

$errorMessage = "Connection cannot be established with the database!";
于 2013-03-30T22:48:49.257 回答