Jump to content

I don't know why my checkbox isn't working.


Thaikhan

Recommended Posts

Hey guys,

 

I'm struggling to find why my checkbox isn't posting as set. I would really appreciate it if someone could tell me what I'm doing wrong.

 

Here's the form:

	$form = "<form action='./register.php' method='post'>
	<table>
	<tr>
		<td></td>
		<td><font color='red'>$errormsg</font></td>
	</tr>
	<tr>
		<td>Username:</td>
		<td><input type='text' name='user' value='$getuser' /></td>
	</tr>
	<tr>
		<td>Email:</td>
		<td><input type='text' name='email' value='$getemail' /></td>
	</tr>
	<tr>
		<td>Password:</td>
		<td><input type='password' name='pass' value='' /></td>
	</tr>
	<tr>
		<td>Retype Password:</td>
		<td><input type='password' name='retypepass' value='' /></td>
	</tr>
	<tr>
		<td>Receive Forum Notifications:</td>
		<td><input type='checkbox' name='fn' value='1' /></td>
	</tr>
	<tr>
		<td></td>
		<td><input type='submit' name='registerbtn' value='Register' /></td>
	</tr>
	</table>
	</form>";

And here's the beginning of the page:

<?php
error_reporting(E_ALL ^ E_NOTICE);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<title>Member System - Register</title>
		<link href="./style.css" rel="stylesheet" type="text/css" />
	</head>
<body>
<?php

	if ( $_POST['registerbtn'] ) {
		$getuser = $_POST['user'];
		$getemail = $_POST['email'];
		$getpass = $_POST['pass'];
		$getretypepass = $_POST['retypepass'];
		if (isset($_POST['fn'])) {
			echo "checked";
			exit();
		}
		else
			echo "unchecked";
			exit();

Thanks so much!!

My problem is that it's not setting the variable. Any ideas?

if ( $_POST['registerbtn'] ) {
	$getuser = $_POST['user'];
	$getemail = $_POST['email'];
	$getpass = $_POST['pass'];
	$getretypepass = $_POST['retypepass'];
		if (isset($_POST['fn'])) {
			$fn == 1;
		}
		else {
			$fn == 0;
		}

	echo $fn;
	exit();

Nothing is being echoed...

There's no mysql_error and the browser isn't coming up with anything. Neither is the server log.

 

No, that's impossible or you have a logical error somewhere in the script.

 

My briefly test just works fine:

<?php
error_reporting(-1);

	if (!empty($_POST['registerbtn'])) {
		$getuser = $_POST['user'];
		$getemail = $_POST['email'];
		$getpass = $_POST['pass'];
		$getretypepass = $_POST['retypepass'];
		if (isset($_POST['fn'])) {
			echo "checked";
			exit();
		}
        }
?>
	<form action='index.php' method='post'>
	<table>
	<tr>
		<td></td>
		<td><font color='red'></font></td>
	</tr>
	<tr>
		<td>Username:</td>
		<td><input type='text' name='user' value='' /></td>
	</tr>
	<tr>
		<td>Email:</td>
		<td><input type='text' name='email' value='getemail' /></td>
	</tr>
	<tr>
		<td>Password:</td>
		<td><input type='password' name='pass' value='' /></td>
	</tr>
	<tr>
		<td>Retype Password:</td>
		<td><input type='password' name='retypepass' value='' /></td>
	</tr>
	<tr>
		<td>Receive Forum Notifications:</td>
		<td><input type='checkbox' name='fn' value='1' /></td>
	</tr>
	<tr>
		<td></td>
		<td><input type='submit' name='registerbtn' value='Register' /></td>
	</tr>
	</table>
	</form>

The script below will echo checked or unchecked accordingly, so it's working, but I don't understand why it's not echoing 0 or 1 for var $fn.

	if ( $_POST['registerbtn'] ) {
		$getuser = $_POST['user'];
		$getemail = $_POST['email'];
		$getpass = $_POST['pass'];
		$getretypepass = $_POST['retypepass'];
		if (isset($_POST['fn'])) {
			$fn == 1;
			echo $fn;
			echo "Checked";
			echo mysql_error();
			exit();
		}
		else {
			$fn == 0;
			echo $fn;
			echo "Unchecked";
			echo mysql_error();
			exit();
		}

I am not sure whether the checkbox element accepts a value attribute itself, check at docs, but .. entirely sure that the checked attribute can be used to indicate the default state.

So, you should set this to either true or false.

two == is a comparison operator. your code is not assigning a value to $fn (one =), it's comparing $fn with 1 or 0 and if you had php's error reporting turned full on, you would know this because you would be getting an undefined variable error at the $fn references.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.