开发者

noob: how to show login error message on the same page after php server processes request

开发者 https://www.devze.com 2022-12-27 10:52 出处:网络
I have a login page. User first enters information and submits the form. And I have a php script that will see if the user exists.

I have a login page. User first enters information and submits the form.

And I have a php script that will see if the user exists.

If( authenticated == true)  
{  
    // I do a redirect  
}  
else  
{  
  // I want to popup an error message on the same p开发者_如何学Cage.  
} 

1) I'm not sure how to show the popup message, I would like to make my div element visible with an error message returned from the server,

I would have to use Ajax, right? But how?

Or are there alternatives which are just as good.


if you would use ajax, use it for the whole process. So, no such problems at all. Get response and show it to user.

But as you stated yourself as a newbie, I'd strongly advise you to do it straight and simple way, just to learn how the things are.

As a general rule, a redirect always preferred after POST method request. In some cases one can omit this, but for the login form you would use a session anyway. So, you can start a session, write an error information there and then do Location: redirect.

After it, check session for the errors, and then notify user using any method you wish: a div, or a popup or anything.


If the form is posted to the same page, why not just use variables to display the error messages?

<?php

if (strlen($_POST['text']) < 5)
{
 $error['text'] = "Too short";
}

?>

<?php

if (isset($error['text']))
{
// Print errors at each form element?
}

?>

Hide & show DIVs using Javascript: http://csscreator.com/node/708


I would put the login page in a separate .HTML that you can run

$login = file_get_contents('login.html);

and in that markup put <!-- ERROR --> which you can then run a

$login = str_replace('<!-- ERROR -->', $error, $login); 

to insert any text you want and when you're all done

print $login;


The simplest answer is to just place a php if condition where you want the message to appear. No need for ajax.

some text above

<?php if (! $authenticated) { echo '<div class="error">'.$your_error_message.'</div>'; } ?>

some text below

I don't know what $authenticated in your case means, but make sure it includes a check that there were post variables also, otherwise the error message will show up on the login page in the pre-submitted use case.


Possibly simplest way to

function render_auth($errors = false){
?>
   <form action="/someURL">
     <? if($errors) echo $errors ?>
     <label>User name<input name="user_name"/></label><br/>
     <label>Password<input name="user_password" /></label><br/>
     <input type="submit"/>
   </form>
<?
};

On first call to the authentication page/controller you'd call render_auth() then on processing the auth call, if there was an error you'd call it render_auth("Missing password.") or such.

Generally stuff like this is pretty trivial if you're using a Templating system for you're views but if not, this function emulates templating to an extent.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号