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? Link to comment https://forums.phpfreaks.com/topic/41394-form-load-dont-want-to-execute-particular-code/ 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. Link to comment https://forums.phpfreaks.com/topic/41394-form-load-dont-want-to-execute-particular-code/#findComment-200528 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> Link to comment https://forums.phpfreaks.com/topic/41394-form-load-dont-want-to-execute-particular-code/#findComment-200532 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 Link to comment https://forums.phpfreaks.com/topic/41394-form-load-dont-want-to-execute-particular-code/#findComment-200535 Share on other sites More sharing options...
newbienewbie Posted March 6, 2007 Author Share Posted March 6, 2007 thanks kenrbnsn, it worked Link to comment https://forums.phpfreaks.com/topic/41394-form-load-dont-want-to-execute-particular-code/#findComment-200540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.