Click to See Complete Forum and Search --> : Radio Input Not Being Recognized


Joseph Witchard
10-05-2009, 12:20 AM
<form id="user_form" name="user_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return checkUserInput();">
<label for="username">Username:</label><br/>
<input type="text" id="username" name="username" maxlength="25"/><br/><br/>
<label for="email">Email Address:</label><br/>
<input type="text" id="email" name="email" maxlength="40"/><br/><br/>
<label>Is this user an Admin?</label> Yes<input type="radio" name="admin" value="1"/> No<input type="radio" name="admin" value="0"/><br/><br/>
<label for="pwd">Please enter your password:</label><br/>
<input type="password" id="pwd" name="pwd"/><br/><br/>
<input type="submit" id="submit" name="submit" value="Submit"/> <input type="reset" value="Reset"/>

I'm using PHP to process the form, but I don't think that's what's working against me. For some reason, if the No radio button is selected, PHP tells me that I haven't filled out everything (even when I have). The reason I don't think it's something wrong with my PHP is that it knows that everything is filled out if I select the Yes button. For some reason it just doesn't seem to register when No is selected.

criterion9
10-05-2009, 12:37 AM
Can you post some of the PHP code since the html part looks fine to me?

Joseph Witchard
10-05-2009, 12:46 AM
/** Coded by: Jeffrey (Joseph Witchard)
** Created on: 10/04/09
** Last modified: 10/05/09
** Purpose: To allow Rebirth Admins
** the ability to add new
** users to the Rebirth
** News System. */

// make sure the user is logged in

require('includes/valid_user.php');

// include updating.php

require('../includes/updating.php');

// include the connection settings

require('includes/SI_conn.php');

// include all the scripts needed

require('includes/pwd_crypt.php'); // used to encrypt passwords
require('includes/pwd_generator.php'); // used to generate a random password for new users
require('includes/mail_mysqli_conn_error.php'); // used to notify me if there's a MySQLi connection error
require('includes/mail_mysqli_stmt_error.php'); // used to notify me if there's an error with MySQLi prepared statements

// see if the form has been submitted

if (array_key_exists('submit', $_POST) && !empty($_POST['submit']))
{

// establish the database connection

$conn = @siAccess();

// see if there was a connection error

if (mysqli_connect_errno())
{

// assign the error to a variable

$error = mysqli_connect_error();

// mail it to me

mail_mysqli_conn_error($error, DB_NAME);

// set up a boolean to let the user know

$no_conn = true;

}

else
{

// list expected fields

$expected = array('username', 'email', 'admin', 'pwd');

// create an array for missing elements

$missing = array();

// process the POST variables

foreach ($_POST as $key => $value)
{

// strip whitespace and assign to a temporary variable

$temp = trim($value);

if (empty($temp) && in_array($key, $expected))
{

// add to missing

$missing[] = $key;

}

else
{

// assign to a variable of the same name

${$key} = $temp;

}

}

// go ahead only if missing is needed

if (empty($missing))
{

// missing is no longer needed

unset($missing);

// crypt the Admin's password

$pwd = pwd_crypt($pwd);

// check the username

if (!preg_match('/^[a-z ]+$/i', $username))
{

// set up a boolean to let the user know

$bad_name = true;

}

elseif (strlen($username) < 6)
{

// set up a boolean to let the user know

$short_name = true;

}

elseif ($pwd !== $_SESSION['pwd'])
{

// set up a boolean to let the user know

$no_pwd = true;

}

else
{

// assign the user's password to a variable

$user_pwd = pwd_generator();

// assign an encrypted version of the user's password to a variable to store in the database

$db_pwd = pwd_crypt($user_pwd);

// use pwd_generator to create a token for the user

$token = pwd_generator();

// convert the admin status to an integer

$admin = (int)$admin;

// the user is not Jeffrey

$jeffrey = 0;

// set up the query

$query = "INSERT INTO users (admin, token, jman, username, pwd, user_email) VALUES (?, ?, ?, ?, ?, ?)";

// prepare the statement

$stmt = $conn->prepare($query);

// bind the parameters

$stmt->bind_param('isisss', $admin, $token, $jeffrey, $username, $db_pwd, $email);

// execute

$stmt->execute();

// commit

$conn->commit();

// check to see if there was a statement error

if ($stmt->errno)
{

// assign the error to a variable

$error = $stmt->error;

// mail me the error

mail_mysqli_stmt_error($error, DB_NAME);

// set up a boolean to let the user know

$no_stmt = true;

// close everything

$stmt->close();

$conn->close();

}

elseif ($stmt->affected_rows > 1)
{

// mail the user their login information

$to = $email;
$subject = 'Your Rebirth News System Login Information';
$headers = "From: Rebirth Staff <staff@hogwarts-rpg.net> \r\n";
$headers .= "Reply-To: Rebirth Staff <staff@hogwarts-rpg.net> \r\n";
$message = "Hello! A Rebirth Administrator has set up your accesses to the Rebirth News System.\n\n";
$message .= "Username: $username\n";
$message .= "Password: $user_pwd\n\n";
$message .= "You can change your password after you log in. You can log in at https://hogwarts-rpg.net/staff. ";
$message .= "Please enter the URL into your browser EXACTLY as it is written. Otherwise it will not work.\n\n";
$message .= "Please reply to this email if you have any trouble!\n\n";
$message .= "Sincerely,\nThe Rebirth Staff";

$mail_sent = mail($to, $subject, $message, $headers);

// set up a boolean to let the user know

$inserted = true;

// close everything

$stmt->close();

$conn->close();

}

}

}

}

}


Thanks.

criterion9
10-05-2009, 07:30 AM
Is it possible that the value of "0" is popping false on the is_empty? See if you can create the smallest file that still has the trouble so we can figure out what it might be.

Joseph Witchard
10-05-2009, 01:08 PM
What exactly do you mean by "smallest files"?

Nihiliste
10-05-2009, 02:04 PM
The problem is with the "0" value related to the No radio:


php > $string = "0";
php > var_dump( empty( $string ) );
bool(true)


So when you do:


if (empty($temp) && in_array($key, $expected))


The empty() test returns true.

So my suggestion is to change the value associated with the No radio.


MGB