Jump to content

Undefined index: flag


Shadow_Walker

Recommended Posts

Notice: Undefined index: flag in C:\xampp\htdocs\a\Student_login.php on line 47

Notice: Undefined index: flag in C:\xampp\htdocs\a\Student_login.php on line 53

Notice: Undefined index: flag in C:\xampp\htdocs\a\Student_login.php on line 58

 

What's wrong?


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Login Page</title>
<link rel="stylesheet" href="Style.css" type="text/css"/>
<script type="text/javascript">
function validate() 
{
	if(document.form1.st_id.value=="")
	{
	  alert("Please enter your login Id.");
	  document.form1.st_id.focus();
	  return false;
	}
	if(document.form1.st_pass.value=="")
	{
	  alert("Please enter your password.");
	  document.form1.st_pass.focus();
	  return false;
	}
}
</script>
<style type="text/css">
<!--
body {
	background-color: #CCCCFF;
}
-->
</style></head>
<body onLoad="javascript:document.form1.st_id.focus()">
<form name="form1" method="post" action="Student_login_handler.php" onSubmit="return validate();">
  <table width="100%" height="100%" >
    <tr>
      <td height="15%"><?php include 'Header.php';?></td>
    </tr>
    <tr>
      <td width="100%" height="80%" align="center" valign="baseline"><table width="90%" >
        <tr>
          <td width="8%"><a href="index.php" class="stylelink" style="text-decoration:none; font-family: "Times New Roman", Times, serif;">Home</a></td>
          <td width="35%" align="center"> </td>
          <td width="27%"> </td>
          <td width="30%" align="right"><a href="Student_Registration.php" class="stylelink" style="text-decoration:none ; font-weight: bold;">New Student Click Here</a></td>
        </tr>
        
        
        
          <?php if($_GET['flag'] == "success") { ?>
		  <tr>
		       <td class="stylegreen" colspan="4" align="center">Congratulations! You Are successfully registered. You can use your Login Id and Password to login to your account.</td>
		  </tr>
		  <?php 
		  }
		  else if($_GET['flag'] == "exists") { ?>
		  <tr>
		       <td class="stylered" colspan="4" align="center">This Login Id( <?=$_GET['student_id']?>) already exists.Please, try again with another Login Id</td>
		  </tr>
		  <?php 
		  	}else if($_GET['flag'] == "error") {
		  ?>
         	<tr>
		       <td class="stylered" colspan="4" align="center">Error while inserting data. Please, try again.</td>
		  </tr>
		  <?php
		  	}
		  ?>
        
        <tr>
          <td colspan="4"><table width="30%"  border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#CCCCCC" bgcolor="#CCCCCC">
            <tr align="center" bgcolor="#999999">
              <td colspan="2" class="stylebig">Student Login Here</td>
                </tr>
            <tr bgcolor="#E1E1E1" class="stylesmall">
              <td width="35%" align="left" class="style7">Login Id : </td>
                <td width="65%" align="left"><input name="st_id" type="text" id="st_id"></td>
              </tr>
            <tr bgcolor="#E1E1E1" class="stylesmall">
              <td align="left" class="style7">Password:</td>
                <td align="left"><input name="st_pass" type="password" id="st_pass"></td>
              </tr>
            <tr bgcolor="#E1E1E1">
              <td colspan="2" align="center"> 
			 	<?php if($_GET['flag'] == "invalid") { ?>
                <span class="stylered">Invalid Login Id or Password</span>
				<?php }?>				</td>
            </tr>
            <tr bgcolor="#E1E1E1">
              <td colspan="2" align="center"><input name="login" class="style10" type="submit" id="login" value="Login">
                <input name="close" type="button" id="close" class="style10" value="Close" onClick="self.location='index.php'">                </td>
                </tr>
          </table></td>
          </tr>
      </table></td>
    </tr>
    <tr>
      <td height="5%" align="center"><?php include 'Footer.php';?></td>
    </tr>
  </table>
</form>
</body>
</html>
Link to comment
Share on other sites

This is the handler.

<?php
	session_start();
	include 'Connect.php';
	$flag = "";
	$student_id = $_POST['st_id'];
	$st_pass = $_POST['st_pass'];
	$query = "select last_login_date from student_information where student_id='$student_id' and student_password='$st_pass' and student_status ='Enable'";
	$result = mysql_query($query,$link_id);
	if(mysql_error() != null){
		die(mysql_error());
	}
	if($date = mysql_fetch_array($result))
	{
		 $lastdate = $date['last_login_date'];
		 $date2 = date("d-m-Y h:i A",strtotime($lastdate));
		 $_SESSION['userid'] = $_POST['st_id'];
		 $_SESSION['lastlogin'] =$date2;
		 $_SESSION['type'] = "Student";
		 mysql_query("update student_information set last_login_date=now() where student_id='$student_id'",$link_id);
		 if(mysql_error() != null){
			die(mysql_error());
		}
		 header("location:Student_Home.php");
		 die();
	}
	else
	{
		$flag = "invalid";
		header("location:Student_login.php?flag=$flag");
		die();		
	} 
	
?>

Link to comment
Share on other sites

You should defined this index first. Then you could use isset() or empty() functions ( see differences b/w them in the links above) to check 1)if the index is being set 2) what value is being set to it and finaly use if, if else, else constructs to check if the value is equal to value you're expecting to be. That's all and it's simple.  

Link to comment
Share on other sites

English please :-)

 

Sorry, if you were being confused with it. I'm working on improving my English by visiting phpfreaks every day :)

 

 

does it mean i'll need to check another page or could be the index page and it should contain value???

 

Just because you're able to write something in your script doesn't mean it will work as expected and the programming cannot work in this way.

Looking at your two scripts, I see ONLY one possible situation when this index could be defined in case, next else condition is gonna be "TRUE":

else {
$flag = "invalid";
header("location:Student_login.php?flag=$flag");
}

Here, the word "flag" after a question mark it's an index of $_GET['flag'] variable and its value is equal to $flag (invalid).

 

You can get into an url (string) that contains a parameter (index) but no value to it, like so:

 

Student_login.php?flag

 

In case like this you would use isset() or empty() functions to determine how "flag" is valued.

if(isset($_GET['flag'])) echo 'flag is set';

if(!empty($_GET['flag'])) echo 'flag is not empty';

Or, like in the example provided by you. The following could go to true only if the index exist and its value is equal to "success"

 

$flag="success";

 

Student_login.php?flag=$flag

 <?php if($_GET['flag'] == "success") { ?>

<tr>
    <td class="stylegreen" colspan="4" align="center">Congratulations! You Are successfully registered. You can use your Login Id and Password to login to your account.</td>
    </tr>

// or better option using empty() function

if(!empty($_GET['flag']) && $_GET['flag'] == "success") {
    
    // do something
}
Link to comment
Share on other sites

You're getting these Notices because at runtime, you are not passing the url parameter ..... your site?flag=some_value

 

A notice is not necessarily an error, but your code tends to be more reliable if you work to eliminate them. But it does not necessarily mean your code won't work right. You can turn down the error level to exclude notices and many production sites do exactly that.

 

<?php if($_GET['flag'] == "success") { ?>
A couple of stylistic suggestions.

 

1. When you have if-then-else-otherwise-etc blocks, the code will be cleaner if you use a switch statement.

 

2. To check whether an array contains a particular key you have 2 primary options: array_key_exists or isset.

 

 

Putting this into practice:

 


if (isset($_GET['flag'])) {
    switch ($_GET['flag'])
        case 'success':
            // Success code here
            break;
        case 'exists':
            // exists code here
            break;
        case 'error':
            // error code here
            break;
        default:
            // If the code got here, then flag was something you didn't expect.
            // What do you need to do in that case?
    }

}
Link to comment
Share on other sites

To gizmola and jazzman 1,,

 

That was a very big help,,i combine both of your ideas and find the best solution that fits,,and it turns out both of yours insights were right,,

 

thank you so much guys and more power..

 

this problem is SOLVED until further discoveries as i go on to finish the whole information system

Link to comment
Share on other sites

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.