从选择标签 PHP 获取帖子值

发布于 2024-12-06 02:15:27 字数 5895 浏览 1 评论 0原文

嘿,我需要从选择标签中选择性别 然后将该值存储为 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))
   {
// [email protected] passes
// [email protected] 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";
    }

    ?>

我在这里缺少什么?伙计们?,

我一直在寻找 google 找到答案没有成功。

这是 php 给我的错误:

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

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

Hey I need to get the gender selected from a select tag
and then store that value as a variable in php, here's some relevant snippets of both
the register.php form and the register_process.php file

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>

there is a ton of javascript I have omitted from this that does very basic validation

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})

what am I missing here guys?,

I have been hunting around
google to find an answer with no success.

heres the error php is giving me:

"Notice: Undefined index: gender in register_process.php on line 22"

Everything else in the form IS working fine, there are no other issues

,$email)) { // [email protected] passes // [email protected] 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"; } ?>

what am I missing here guys?,

I have been hunting around
google to find an answer with no success.

heres the error php is giving me:

"Notice: Undefined index: gender in register_process.php on line 22"

Everything else in the form IS working fine, there are no other issues

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北音执念 2024-12-13 02:15:27

你的错误是这样的:

 />

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

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

Your mistake is this:

 />

Don't close your form tag way up there - you need to close it after the select.

BTW xhtml is dead, don't use it (it has been superseded by html5). Use plain HTML and don't close tags that don't need it.

纵情客 2024-12-13 02:15:27

由于您的 HTML 语法错误。

更改:

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

至:

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

Because of error in your HTML syntax.

Change:

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

To:

<form action="register_process.php" method="post">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文