Jump to content

form load - dont want to execute particular code


newbienewbie

Recommended Posts

don't know why it is loading before clicking submit button plz check full code here login.php

 

<html>
<body>

<form action="" method="post">
<center>
Username:
<input name="username" type="text" size="10" maxlength="30"/>
Password:
<input name="password" type="password" size="10" maxlength="30" />
<input type="submit" value="Login" />
</form>
</center>

<?php
$mysql_host='localhost';
$mysql_user='xyz';
$mysql_password='xyz';
$my_database='xyz';
$my_table='users';


// Connecting, selecting database
$link = mysql_connect($mysql_host,$mysql_user, $mysql_password)
    or die('Could not connect: ' . mysql_error());

mysql_select_db($my_database) or die('Could not select database');

// Performing SQL query
$query = "SELECT user FROM $my_table";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
//echo "<table>\n";

$usertrace = '';
$numRows = mysql_num_rows($result);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
    if ($line['user'] == $_REQUEST['username'])
    {
       $user = $line['user'];
       $usertrace=1;
       $query = "SELECT pass FROM $my_table WHERE user='$user'";
       $result=mysql_query($query) or die('Query failed: ' . mysql_error());
       while($row = mysql_fetch_array($result))
       {
           if ($row['pass'] == $_REQUEST['password'])
		{
    		echo "Welcome!";
		}
		else
		{
                echo "\nInvalid Password";
	 	}
       }
}
}

if ($usertrace !== '1')
{
echo "\n Invalid User";
}

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

</body>
</html>

Link to comment
Share on other sites

You only want to execute the code after the form has been submitted.

 

Add a name to the "submit" button, then you can test whether the form was submitted:

<html>
<body>

<form action="" method="post">
<center>
Username:
<input name="username" type="text" size="10" maxlength="30"/>
Password:
<input name="password" type="password" size="10" maxlength="30" />
<input type="submit" value="Login" name="submit_button"/>
</form>
</center>

<?php
if (isset($_POST['submit_button'])) {
   $mysql_host='localhost';
   $mysql_user='xyz';
   $mysql_password='xyz';
   $my_database='xyz';
   $my_table='users';


// Connecting, selecting database
   $link = mysql_connect($mysql_host,$mysql_user, $mysql_password)
       or die('Could not connect: ' . mysql_error());

   mysql_select_db($my_database) or die('Could not select database');

// Performing SQL query
   $query = "SELECT user FROM $my_table";
   $result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
//echo "<table>\n";

   $usertrace = '';
   $numRows = mysql_num_rows($result);
   while ($line = mysql_fetch_assoc($result))
   {
       if ($line['user'] == $_POST['username'])
       {
          $user = $line['user'];
          $usertrace=1;
          $query = "SELECT pass FROM $my_table WHERE user='$user'";
          $result=mysql_query($query) or die('Query failed: ' . mysql_error());
          $row = mysql_fetch_array($result);
          if ($row['pass'] == $_REQUEST['password'])
    	echo "Welcome!";
  else
                echo "\nInvalid Password";
       }
   }

   if ($usertrace !== '1')
echo "\n Invalid User";

// Free resultset
   mysql_free_result($result);

// Closing connection
   mysql_close($link);
}
?>
</body>
</html>

 

Ken

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.