/    Sign up×
Community /Pin to ProfileBookmark

Radio Input Not Being Recognized

[code=html]
<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”/>[/code]

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.

to post a comment
HTML

5 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Oct 05.2009 — Can you post some of the PHP code since the html part looks fine to me?
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorOct 05.2009 — [code=php]
/** 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 <[email protected]> rn";
$headers .= "Reply-To: Rebirth Staff <[email protected]> rn";
$message = "Hello! A Rebirth Administrator has set up your accesses to the Rebirth News System.nn";
$message .= "Username: $usernamen";
$message .= "Password: $user_pwdnn";
$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.nn";
$message .= "Please reply to this email if you have any trouble!nn";
$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();

}

}

}

}

}
[/code]


Thanks.
Copy linkTweet thisAlerts:
@criterion9Oct 05.2009 — 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.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorOct 05.2009 — What exactly do you mean by "smallest files"?
Copy linkTweet thisAlerts:
@NihilisteOct 05.2009 — The problem is with the "0" value related to the No radio:

[code=php]
php > $string = "0";
php > var_dump( empty( $string ) );
bool(true)
[/code]


So when you do:

[code=php]
if (empty($temp) && in_array($key, $expected))
[/code]


The empty() test returns true.

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


MGB
×

Success!

Help @Joseph_Witchard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...