14 Replies - 8670 Views - Last Post: 03 August 2011 - 05:44 PM Rate Topic: -----

#1 ahsanalishahid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-October 10

Php $_POST error undefined index

Posted 03 August 2011 - 01:51 PM

I am getting this error when I test this.

Notice: Undefined index: remember in J:\xampp\htdocs\a\fbook\login.php on line 7



Here is the code.
<?php

require_once('connection.php');
if( isset($_POST['login']) )
{
	$user = htmlentities(mysql_real_escape_string( $_POST['username'] ) );
	$pass = htmlentities(mysql_real_escape_string( $_POST['password'] ) );
	$remember = $_POST['remember'];
	
	if($remember == "on")
	{
		$remember = TRUE;
	}
	elseif($remember == "")
	{
		$remember = FALSE;
	}

}

?>
	



I am unable to figure out why I am getting this error. Help me figure out what has happened.


Regards.

Is This A Good Question/Topic? 0
  • +

Replies To: Php $_POST error undefined index

#2 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: Php $_POST error undefined index

Posted 03 August 2011 - 01:52 PM

It seems as if the url doesn't look like thefile.php?remember=on
Was This Post Helpful? 0
  • +
  • -

#3 thephpdev  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 59
  • Joined: 01-July 11

Re: Php $_POST error undefined index

Posted 03 August 2011 - 01:54 PM

Can we see the form code?
Was This Post Helpful? 0
  • +
  • -

#4 ahsanalishahid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-October 10

Re: Php $_POST error undefined index

Posted 03 August 2011 - 01:54 PM

@Envowner

I am using Post method. Here is the html code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="login.php" method="post">
<BR>Username:  <input type = "text" name = "username" >
<BR>Password: <input type = "password" name = "password">
<BR><input type="checkbox" name="remember" >Remember me ?
<BR><input type = "submit" value = "Login" name= "login">



</body>

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#5 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: Php $_POST error undefined index

Posted 03 August 2011 - 01:55 PM

The action of the form should be what ever the filename of the file is with ?remember=whateveritshouldbe, I believe

This post has been edited by EnvXOwner: 03 August 2011 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 thephpdev  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 59
  • Joined: 01-July 11

Re: Php $_POST error undefined index

Posted 03 August 2011 - 01:56 PM

Okay, well the remember isn't passed if it isn't checked. So you should add this code in place of the $remember declaration

if (isset($_POST['remember'])) $remember = $_POST['remember']; else $remember = "off";


or something similar to that.
Was This Post Helpful? 1
  • +
  • -

#7 ahsanalishahid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-October 10

Re: Php $_POST error undefined index

Posted 03 August 2011 - 01:57 PM

@EnvXOwner

I am using post method then how come can it come in the url? :/
Was This Post Helpful? 0
  • +
  • -

#8 thephpdev  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 59
  • Joined: 01-July 11

Re: Php $_POST error undefined index

Posted 03 August 2011 - 01:58 PM

Your right, the post method would be invisible in the address bar. UIf your using get it'll be in the address bar
Was This Post Helpful? 0
  • +
  • -

#9 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: Php $_POST error undefined index

Posted 03 August 2011 - 01:58 PM

Well, I was talking about the action of the form, do as thephpdev said and see how it goes.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Php $_POST error undefined index

Posted 03 August 2011 - 02:02 PM

remember is (or should be) coming in through POST. It will therefore NOT be in the URL.
Was This Post Helpful? 0
  • +
  • -

#11 ahsanalishahid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-October 10

Re: Php $_POST error undefined index

Posted 03 August 2011 - 02:03 PM

@ thephpdev

yes doing it your way works :D Thanks for help :) . Now a question has come into my mind that if a user leaves the checkbox empty, what value will be passed to the POST array?
Was This Post Helpful? 0
  • +
  • -

#12 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: Php $_POST error undefined index

Posted 03 August 2011 - 02:04 PM

JackOfAllTrades is correct. It's a post method therefore there will be nothing in the URL. sorry.
Was This Post Helpful? 0
  • +
  • -

#13 thephpdev  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 59
  • Joined: 01-July 11

Re: Php $_POST error undefined index

Posted 03 August 2011 - 02:04 PM

An empty checkbox isn't submitted through GET or POST
Was This Post Helpful? 2
  • +
  • -

#14 calebjonasson  Icon User is offline

  • $bert = new DragonUnicorn(); $bert->rawr();
  • member icon

Reputation: 209
  • View blog
  • Posts: 989
  • Joined: 28-February 09

Re: Php $_POST error undefined index

Posted 03 August 2011 - 04:34 PM

As thephpdev said you are going to need to check to see if it is set.

$remember = null;
if(isset($_POST['remember']))
{
    $remember = true;
}else{
    $remember = false;
}



Was This Post Helpful? 1
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Php $_POST error undefined index

Posted 03 August 2011 - 05:44 PM

Or a one-liner:
$remember = isset($_POST['remember']);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1