从选择标记PHP获取post值

时间:2011-09-22 08:51:30

标签: php html select post

嘿我需要从选择标签中选择性别 然后将该值存储为php中的变量,这里是两者的一些相关片段 register.php表单和register_process.php文件

register.php

    <form action="register_process.php" method="post">
    <table>
        <tr>
            <td>Username (to be used for login and display name)</td>
            <td><input type="text" name="username" id="username" onclick="check()"/></td>
            <td id="username_check"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password" id="password" onclick="check()"/></td>
            <td id="password_check"></td>
        </tr>
        <tr>
            <td>Password (Re-Enter)</td>
            <td><input type="password" name="repassword" id="repassword" onclick="check()"/></td>
            <td id="repassword_check"></td>
        </tr>
        <tr>
            <td>Email Address</td>
            <td><input type="text" name="email" id="email" onclick="check()"/></td>
            <td id="email_check"></td>
        </tr>
        <tr>
            <td>Email Address (Re-Enter)</td>
            <td><input type="text" name="reemail" id="reemail" onclick="check()"/></td>
            <td id="reemail_check"></td>
        </tr>
        <tr>
            <td>Gender</td>
            <td>
                <select name="gender">
                    <option value="1">Male</option>
                    <option value="2">Female</option>
                </select>
            </td>
            <td></td>
        </tr>
        <tr>
            <td>I agree to the terms and conditions</td>
            <td><input type="checkbox" name="tos" id="tos" /></td>
            <td id="tos_check"></td>
        </tr>
        <tr>
            <td id="valid" colspan="3"></td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" value="Register" /></td>
        </tr>
        <tr>
            <td colspan="3"><a href="login.php">Cancel</a></td>
        </tr>
    </table>
</form>

我从这里省略了大量的javascript做了非常基本的验证

register_process.php

    <?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
    ?>
    <?php
    $connection = mysql_connect(HOST, USERNAME, PASSWORD);
    if(!$connection)
    {
die("Database connection failed: " . mysql_error());
    }
    $db_select = mysql_select_db(DATABASE, $connection);
    if(!$db_select)
    {
die("Database selection failed: " . mysql_error());
    }

    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $repassword = mysql_real_escape_string($_POST['repassword']);
    $email = mysql_real_escape_string($_POST['email']);
    $reemail = mysql_real_escape_string($_POST['reemail']);
    $gender = $_POST['gender'];
    $tos = mysql_real_escape_string($_POST['tos']); // not being checked yet

    $errors = 0;
    $success = 0;

    // USERNAME CHECK
    if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) 
    {
    $user_query = "SELECT * FROM users WHERE user_name = '$username' OR login_name   =  '$username' LIMIT 1";
$result=mysql_query($user_query);
$count=mysql_num_rows($result);
if($count==0)
{
    echo "username is available <br/>";
    $success++;
    echo "<br/>1 Passed<br/>";
}
else 
{
    echo "sorry, that username already exist";
    $errors++;
    echo "<br/>1 Passed<br/>";
}

   }
    else 
    {
     echo "You either need to enter a username, or you have entered a username in an incorrect format.";
 $errors++;
 echo "<br/>1 Passed<br/>";

}

   // PASSWORD CHECK
   if(preg_match('/^[a-z\d_]{5,20}$/i', $password))
   {
// password is between 5-10 characters, alpha-numeric (a-z, A-Z, 0-9) and underscores
if($password === $repassword)
{
    // password is identical
    $success++;
    echo "<br/>2 Passed<br/>";
}
else 
{
    // passwords do not match
    $errors++;
    echo "<br/>2 Passed<br/>";
}
 }
     else 
     {
  echo "Password failed validation";
  $errors++;    
 }

   // EMAIL CHECK
   if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',$email))
   {
// user@email.com passes
// 1@1.com passes
// -@_.com passes
//
echo "<br/> email is ok";
if($email === $reemail)
{
    // email addresses match
    $success++;
    echo "<br/>3 Passed<br/>";
}
else 
{
    // email address does not match
    echo "<br/>3 Passed<br/>";
    $errors++;
}
   }
    else
    {
echo "email validation failed <br/>";
$errors++;
echo $email;    
   }
    // Here is the problem, I can't seem to evaluate the correct value,
    // When I echo out $gender I get nothing, So theres either an issue
    // in the html form OR in the way I use $gender = $_POST['gender'];

    if($gender == 1 || $gender == "1" || $gender == '1')
    {
    echo "male selected";
    }
    else 
    {
    echo "female selected";
    }

    ?>

我在这里想念的是什么?

我一直在打猎 谷歌找到一个没有成功的答案。

继承人php给我的错误:

“注意:未定义的索引:第22行的register_process.php中的性别”

表单中的其他所有内容都运行正常,没有其他问题

2 个答案:

答案 0 :(得分:5)

你的错误是:

 />

请勿在此处关闭表单标记 - 您需要在选择后关闭它。

BTW xhtml已经死了,不要使用它(它已被html5取代)。使用纯HTML,不要关闭不需要它的标签。

答案 1 :(得分:0)

由于HTML语法错误。

变化:

<form action="register_process.php" method="post" />

要:

<form action="register_process.php" method="post">