开发者

Get post value from a select tag PHP

开发者 https://www.devze.com 2023-04-06 20:37 出处:网络
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

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})$',$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";
    }

    ?>

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_pr开发者_如何学Goocess.php on line 22"

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


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.


Because of error in your HTML syntax.

Change:

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

To:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号