heritage1 Posted December 9, 2008 Share Posted December 9, 2008 I want one button to have two actions: Check username and password by setting the form handler to itself. If incorrect, no change but if correct I set the form handler to the desired page. My code 'works' if the button is clicked twice. Can anyone suggest how to accomplish this on the first click? {Seems like a standard problem but I have looked and googled for a long time} Stripped down code for sampleLogin.php <?php // change form handler on the fly $username = $_POST [ 'username' ]; $password = $_POST [ 'password' ]; if ( $username == 'rightName' && $password == 'rightPassword' ) { $formHandler = 'handler1.php'; } elseif ($username) { $formHandler = 'sampleLogin.php'; $msg = 'Username and Password not recognized.'; } // end if !$username ... ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <meta http-equiv="Content-Type" content="text/css; charset=iso-8859-1"> <html> <head> <TITLE> Login Form </TITLE> </html> <body> <form method="post" id="form1" action="<?php echo $formHandler; ?>"> <table width="40%" border="1"> <tr> <td> Username </td> <td> <input name="username" type="text" value="<?php echo $username; ?>" > </td> </tr> <tr> <td> Password </td> <td> <input name="password" type="text" value="<?php echo $password; ?>" > </td> </tr> <tr> <td align="center" colspan="2"> <input name="submit" type="submit" value="Login"> </td> </tr> </table> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/136227-one-button-two-form-handlers/ Share on other sites More sharing options...
Brian W Posted December 9, 2008 Share Posted December 9, 2008 Here on PHP freaks we put [ code ] [ /code ] (*without the spaces in the tags) tags around code... if U have the "Modify" option, edit you post and highlight the code part of it then click on the bottom above the emoticons that looks like # If you don't make this a habit, your not likely to be helped much. Quote Link to comment https://forums.phpfreaks.com/topic/136227-one-button-two-form-handlers/#findComment-710618 Share on other sites More sharing options...
Maq Posted December 9, 2008 Share Posted December 9, 2008 Yes, please surround your code with the code tags. One question, is this form being submitted to the same page? Looks like this code all belongs to sampleLogin.php... Is that where the form is from in the HTML? Quote Link to comment https://forums.phpfreaks.com/topic/136227-one-button-two-form-handlers/#findComment-710623 Share on other sites More sharing options...
revraz Posted December 9, 2008 Share Posted December 9, 2008 The first time the code runs, your POST variables are empty, which means $formhandler will be $formHandler = 'sampleLogin.php'; So the first time you hit submit, it will process that page because it's set here <form method="post" id="form1" action="<?php echo $formHandler; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/136227-one-button-two-form-handlers/#findComment-710626 Share on other sites More sharing options...
heritage1 Posted December 19, 2008 Author Share Posted December 19, 2008 Thanks for explaining the [ code ] [ /code ] convention. Sorry. I couldn't find any 'Modify' option. Reply #3 must understand although the comment is short. I have my own explanation now and reply #3 seems to say the same thing more compactly. Thanks. There is a 'better' way which is to leave php and use the javascript object document.properties and return. I usually don't like in-line javascript but I am willing to compromise on this one. Quote Link to comment https://forums.phpfreaks.com/topic/136227-one-button-two-form-handlers/#findComment-719988 Share on other sites More sharing options...
flyhoney Posted December 19, 2008 Share Posted December 19, 2008 I think maybe what you actually want to do is a header redirect when the username and password are correct: <?php // change form handler on the fly $username = $_POST [ 'username' ]; $password = $_POST [ 'password' ]; if ($username == 'rightName' && $password == 'rightPassword' ) { header('Location : ' . 'handler1.php'); } elseif ($username) { $msg = 'Username and Password not recognized.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136227-one-button-two-form-handlers/#findComment-719997 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.