Jump to content

Recommended Posts

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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/136227-one-button-two-form-handlers/
Share on other sites

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.

 

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; ?>">

 

  • 2 weeks later...

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.

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.';
}
?>

 

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.