Jump to content

[SOLVED] store checkbox option


DeanWhitehouse

Recommended Posts

i have a remember me checkbox (i don't think it works) and a hide email checkbox , how can i store whether its checked or not,(not in the database but on the actual box)

so if a user ticks hide email, when they view there settings again the box is still ticked, bot unticked.

 

Here are both codes the hide email and the remember me code.

<?php
require_once 'db_connect.php';
require_once 'nav_bar.php';
require_once 'logged_in.php';

if ($_SESSION['is_valid'] == true){
$user_id = $_SESSION['user_id'];
$sql = "SELECT * FROM $user WHERE `user_id`='{$user_id}' LIMIT 0,1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$email = $row['user_email'];
$show_email = $row['show_email'];

$user_password = $_POST["user_password"];		
$user_password2 = $_POST["user_password2"];
$user_email = $_POST["user_email"];		
$user_email2 = $_POST["user_email2"];

if(isset($_POST['update']))
{
if($user_email)
{
	if($user_email == $user_email2)
	{
	mysql_query("UPDATE $user 
SET user_email = '$user_email' 
WHERE user_id = '$user_id'")or die('Could not update email: ' . mysql_error());
	}
	else
	{
	echo "Email Addresses do not match";
	}
}
if($user_password)
{
	if($user_password == $user_password2)
	{
	mysql_query("UPDATE $user 
SET user_password = '$user_password' 
WHERE user_id = '$user_id'")or die('Could not change password: ' . mysql_error());
	}
	else
	{
	echo "Passwords do not match";
	}
}
if((isset($_POST['hideemail'])) && ($_POST['hideemail'] == 0))
   {
   mysql_query("UPDATE $user
   SET show_email = '0'
   WHERE user_id = '$user_id'")or die('Could not change settings: ' . mysql_error());
   echo "Settings Saved";
        }
   elseif((isset($_POST['hideemail'])) && ($_POST['hideemail'] == 1))
   {
   mysql_query("UPDATE $user
   SET show_email = '1'
   WHERE user_id = '$user_id'")or die('Could not change settings: ' . mysql_error());
        echo "Settings Saved";
   }
}

?>
<html>
<table bgcolor='#999999' align='center' width="400px">
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='POST'>
<tr><td width="10px">Maximum Length<br /> 20 characters.</td></tr>
<tr><td width="10px">E-mail Address:</td> <td><input type='text' name='user_email' value="<?php echo "$email"; ?>"  /><br /></td></tr>
<tr><td width="10px">Confirm E-mail Address: </td><td><input type='text' name='user_email2' value="<?php echo "$email"; ?>" /><br /></td></tr>
<tr><td width="10px">Maximum Length<br /> 30 characters.</td></tr>
<tr><td width="10px">New Password:</td><td> <input type='password' name='user_password' maxlength="30" /><br /></td></tr>
<tr><td width="10px">Confirm Password:</td><td> <input type='password' name='user_password2' maxlength="30" /><br /></td></tr>
<tr><td>Email Status:</td></tr><td><tr><?php if ($show_email == 1)
{ 
echo "Visible";
}
elseif ($show_email == 0)
{
echo "Hidden";
} ?></td></tr>
<tr><td width="10px">Hide Email</td><td width="10px">Yes<input type="radio" value="0"  name="hideemail"/> No<input type="radio" value="1"  name="hideemail"/> </td></tr>
<tr><td><input type='submit' value='Save Changes' name='update' /></td>
</form>
</table>
</html>
<?php 
}
else
{
echo "Please login to view this page.";
}
?>

 

<?php
if (isset($_GET['logout']))
{
setcookie("cookname", $_SESSION['username'], time() - 3600, "/");
setcookie("cookpass", $_SESSION['user_password'], time() - 3600, "/");
session_unset();
session_destroy();
}
if ($_SESSION['is_valid'] == true)
{
if ($_SESSION['user_level'] == 2)
{
	?>
	<table class='logged_in'><tr><td>
	<p>Welcome, <br><?php echo $_SESSION['username']; ?>
	<br><a href='user_profile.php?id=<?php echo $_SESSION['user_id']; ?>'>User Profile</a><br>
	<a href='user_setting.php'>Settings</a><br>
	<a href="<?php print $_SERVER["PHP_SELF"]; ?>?logout=true">Logout</a><br />
	</td></tr><tr><td>Logged In</td></tr></table></p>
	<?php 
}

if ($_SESSION['user_level'] == 1)
{
	?>
	<table class='logged_in'><tr><td>
	<p>Welcome, <?php echo $_SESSION['username']; ?>
	<br><a href='user_profile.php?id=<?php echo $_SESSION['user_id']; ?>'>User Profile</a><br>	
	<a href='user_setting.php'>Settings</a><br>
	<a href='admin_centre.php'>Admin Area</a><br>
	<a href="<?php print $_SERVER["PHP_SELF"]; ?>?logout=true">Logout</a><br />
	</td></tr><tr><td>Logged In</td></tr></table></p>
	<?php
}
}

else
{
require_once 'db_connect.php';

if ($_SESSION['is_valid'] == false)
{
	if (isset($_POST['login']))
	{

	$user_name = $_POST["user_name"];        
	$user_password = $_POST["user_password"]; 
	$cookiename = forumcookie;   
	$verify_username = strlen($user_name);
	$verify_pass = strlen($user_password);
		if ($verify_pass > 0 && $verify_username > 0)
		{
		$salt = substr($user_password, 0, 2);
		$userPswd = crypt($user_password, $salt);
		$sql = "SELECT * FROM `$user` WHERE user_name='$user_name' AND user_password='$userPswd' LIMIT 1;";
		$result = mysql_query($sql);
			if (mysql_num_rows($result) == 1)
			{
			$row = mysql_fetch_assoc($result);
			$user_level = $row['userlevel'];
				if ($user_level == 1) 
				{
				$login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = 	'$_GET[p]'"));

				$userright = array($login_check['user_name'], $login_check['userlevel']);
				$s_userpass = serialize($userpass);
				$_SESSION['username'] = $row['user_name'];
				$_SESSION['user_password'] = $row['user_password'];
				$_SESSION['user_level'] = $row['userlevel'];
				$_SESSION['user_id'] = $row['user_id'];
				header("Location:http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]);
				$_SESSION['is_valid'] = true;
					 if(isset($_POST['remember']))
					 {
    					  setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
    					  setcookie("cookpass", $_SESSION['user_password'], time()+60*60*24*100, "/");
   			}
			} 
		elseif ($user_level == 2){    
			$login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = '$_GET[p]'"));

			$userright = array($login_check['user_name'], $login_check['userlevel']);
		$s_userpass = serialize($userpass);
	$_SESSION['username'] = $row['user_name'];
	$_SESSION['user_password'] = $row['user_password'];
	$_SESSION['user_level'] = $row['userlevel'];
	$_SESSION['user_id'] = $row['user_id'];
	header("Location:http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]);
	$_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files
	 if(isset($_POST['remember'])){
    	  setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
    	  setcookie("cookpass", $_SESSION['user_password'], time()+60*60*24*100, "/");
   		}
	}
}
else{
	echo "Login failed. Username and Password did not match database entries.";    
}
}

else
{
echo "Form was not completed. Please go back and make sure that the form was fully completed.";    
}
}
$server = str_replace("?logout=true","",$_SERVER['PHP_SELF']);
?> 

<html>
<table bgcolor='#999999' align='right'><form action="<?php echo $server ?>" method='POST'>
<tr><td>Username: </td><td><input type='text' name='user_name' /><br /></td></tr>
<tr><td>Password:</td><td> <input type='password' name='user_password' /><br /></td></tr>
<tr><td><input type="hidden" name="login" value="true"><input type="submit" value="Submit"></td></tr>
<tr><td><input type="checkbox" value="1" name="remember"> Remember Me </td></tr><tr><td><a href="register.php">[Register]</a></td></tr><tr><td><a href="forgot_password.php">[Forgot Password?]</a></td></tr></table>
</form>
</html>

<?php 
mysql_close();
}
else
{
header("Location:http://".$_SERVER[HTTP_HOST]);
}
}
?>

Link to comment
Share on other sites

if you are pulling the information from the database (as im sure your saving this within the database) just have within the

<input <?php if ($row['email'] == 'whatever'){echo 'checked="checked"';}?> type="checkbox" name="showemail"/>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.