3 Replies - 3810 Views - Last Post: 24 August 2011 - 12:57 AM Rate Topic: -----

#1 chukwuemmanuel09  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 60
  • Joined: 18-August 11

Form submission with checkbox...Keep getting undefined Index..

Posted 23 August 2011 - 09:47 AM

Trying to submit this form but i keep getting an error showing undefined Index..RememberMe.
Thanks..


<?php

include "function.php";

if (isset($_POST["login"]))
{
//get the form data
	$User_name = $_POST['Username'];
	$Password = $_POST['Password'];
	$rememberme = $_POST['RememberMe'];
	
	if ($User_name && $Password )
	{
	  $login = mysql_query("SELECT * FROM Agent_Details WHERE User_name='".$User_name."' LIMIT 1", $link);
	  while($row = mysql_fetch_assoc($login))
		{
		    $dbUsername = $row['User_name'];
$dbPassword = $row['Password'];	
if (md5($Password) == $dbPassword)
$loginok = TRUE;
else
$loginok = FALSE;

if($loginok == TRUE)
{

if ($rememberme=="on")
setcookie("User_name",$User_name,time()+7200);
else if ($rememberme == "")
$_SESSION['User_name'] = $User_name;

header("Location:registeredmember.php");
exit();

}
else
die("Incorrect Username/password combination");
			
		}
	
	
	}
	else
	{
	die("Please enter a user name and password");
	}
}
?>




<html>

<head>
	<title>Agent Log in</title>
	 
</head>

<body>


<div id="contact">

<form method="POST"  name="agentForm" action="agentform.php" >

<p>
<h2>Agent Login</h2>
</p>

<p>
<label>Username</label>
<input id="Username" name="Username" maxlength="120" type="text" autocomplete="off"/>
					
</p>

<p>
<label>Password</label>
<input id="Password" name="Password" maxlength="120" type="password" autocomplete="off"/>
</p>

<p>
<input  type="checkbox" name="RememberMe"/> Remember me  <br />
</p>

<div id="buttons">
<input type="submit" name="login"  value="Log In" /> 
</div>




</form>
</div>

<p>
<h3>
<a href="regForm.php">Register? </a>
</h3>
</p>
</body>
</html>





This post has been edited by macosxnerd101: 24 August 2011 - 10:52 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Form submission with checkbox...Keep getting undefined Index..

#2 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: Form submission with checkbox...Keep getting undefined Index..

Posted 23 August 2011 - 11:05 AM

Your checkbox input element with name "Rememberme" is missing a value attribute. I included in the code below.

<p>
<input type="checkbox" name="RememberMe" value="YouForgotMe"/> Remember me <br />
</p>



Also don't forget to enclose your code in code tags!

Cheers.

This post has been edited by Ace26: 23 August 2011 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 330
  • Joined: 11-July 07

Re: Form submission with checkbox...Keep getting undefined Index..

Posted 23 August 2011 - 10:25 PM

Another thing you should know

A checkbox only carries a value if it's checked, however when it's not, the PHP won't register the variable passed.

instead of using

$remember = $_POST['checkbox_variable'];



Try using

$remember = (isset($_POST['checkbox_variable']) ? true : false);



That way, you're not camouflaging any errors, and getting nice yes or no answer.

Hope this helped



~huzi
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Form submission with checkbox...Keep getting undefined Index..

Posted 24 August 2011 - 12:57 AM

View PostAce26, on 23 August 2011 - 07:05 PM, said:

Your checkbox input element with name "Rememberme" is missing a value attribute. I included in the code below.

<p>
<input type="checkbox" name="RememberMe" value="YouForgotMe"/> Remember me <br />
</p>


A value attribute is not required. Browsers will simply send through the string On when there's no value set.

The reason it's undefined is because if the checkbox is not ticked, it doesn't get sent through - Exactly like huzi8t9 said, this is the key to fixing it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1