newbienewbie Posted March 6, 2007 Share Posted March 6, 2007 NewBie: I have a login form. i don't want to executed following code if ($usertrace !== '1') { echo "\n Invalid User"; } on form load. as opening in browser http://127.0.0.1:8080/testing/login.php what should i do to do this? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted March 6, 2007 Share Posted March 6, 2007 Why is it loading with the page instead of executing when they hit the submit button? From it's appearance, you're validating a user's login identity. Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 6, 2007 Author Share Posted March 6, 2007 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> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 6, 2007 Share Posted March 6, 2007 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 Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 6, 2007 Author Share Posted March 6, 2007 thanks kenrbnsn, it worked Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.