【问题标题】:Get post value from a select tag PHP从选择标签 PHP 中获取 post 值
【发布时间】:2011-11-22 15:34:27
【问题描述】:

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

注册.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 中的性别”

表格中的其他一切都运行良好,没有其他问题

【问题讨论】:

  • 您是否使用提交按钮将表单提交到 register_process.php? &lt;button type="submit"&gt;Submit&lt;/button&gt;
  • 您确定这是您使用的确切代码吗?
  • 是的,那里有一个提交,表单中的其他所有内容都可以正常工作
  • 您的表单元素不应看起来像&lt;form .. /&gt;。删除/
  • "这里是 register.php 表单和 register_process.php 文件的一些相关的 sn-ps"

标签: php html select post


【解决方案1】:

你的错误是这样的:

 />

不要关闭你的表单标签 - 你需要在选择后关闭它。

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

【讨论】:

    【解决方案2】:

    因为您的 HTML 语法错误。

    变化:

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

    收件人:

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

    【讨论】:

    • if($gender == 1) 与 PHP 中的 if($gender == '1') 相同。 PHP 进行了松散的比较。严格使用===
    • if 条件无论哪种方式都可以工作。 PHP 将根据需要转换类型。唯一重要的是=== 运算符。
    • 我已经尝试过这些 if($gender == 1 || $gender == "1" || $gender == '1') 它仍然是默认的 else
    • 也改变了上面指出的标记的小问题,如上所述,表单中还有其他几个项目处理得很好,我只是无法获得选择的值跨度>
    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 2016-07-28
    • 2023-03-27
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多