Reputation: 3712
How do I show a message from the php action script of a form?
The form is a user login with these fields.
<div id="register_user_box" class="inline_form" style="position: absolute; top: 20px; right: 10px; <br/>
<span id="user_msg"></span><br/>
<form action="register_user.php" method="post">
<input type="hidden" name="id" id="id" value="add" />
<input type="hidden" name="edit_user" id="edit_user" value="y" />
<table cellspacing="0px"> <tr><td>Username:</td></tr><tr><td>
<input type="text" name="uname" size="30" value="" class="inline_input"/></td> </tr> <tr><td>Email:</td></tr><tr><td>
<input type="text" name="uemail" size="30" value="" class="inline_input"/></td> </tr> <tr><td>Password:</td></tr><tr><td>
<input type="password" name="upass" size="30" class="inline_input"/></td> </tr> <tr><td>Confirm Password:</td></tr><tr> <td>
<input type="password" name="cpass" size="30" class="inline_input"/></td></tr> </table></td></tr> </table> <p>
<input class="button" type="submit" name="register" value="Register" style="float:right;"/></p>
</form>
</div>
The php script register_user.php checks the if the passwords match and shows an error message if they don't. The script checks all the other fields and prints a message if necessary.
<?php
$messages = array( 'usr_cred_req' => 'Must specify username, email, password.',
'usr_name_bad' => 'Bad username selection. Select a different usrename.',
'usr_name_exists' => 'Username selected already exists. Select a different username.',
'usr_email_bad' => 'Bad email selection. Select a different email.',
'usr_email_exists' => 'Email selected already exists. Select a different email.',
'usr_pass_notmached' => 'Passwords do not match. Type passwords again.',
'usr_not_added' => 'User not added.',
'usr_not_updated' => 'User not updated.',
'usr_added' => 'User added.'
);
$username = trim($_REQUEST['uname']);
$email = trim($_REQUEST['uemail']);
$password = md5(trim($_REQUEST['upass']));
$copasswd = md5(trim($_REQUEST['cpass']));
if ( $password != $copasswd ) { echo '<script> $("#usr_msg").html("'.$messages['usr_pass_notmached'].'"); </script>'; return;}
?>
The error message isn't shown and the browser leaves the page. I'd like the browser to stay on the page and add the error message to the span user_msg.
Upvotes: 0
Views: 789
Reputation: 741
Why are you echoing a JS script to display the error message? The way you have it written out it won't display a little popup, you just having it filling the span
. A better way to do it is something like this:
if ( $password != $copasswd ) {
$display_msg = $messages['usr_pass_notmached']
}
Then in your HTML do this:
<span><?php echo $display_msg?></span>
If the variable is empty then nothing is displayed. If you do this for all the fields, then you can ensure all the information from the form is returned on error, whether the actual field IS the error or not, preventing the user from having to retype all the information again. Have the form do a $_SERVER['PHP_SELF']
to ensure it reloads the same page
I would build the PHP functionality (which can't be turned off by the user) and ensure your form works properly 100% (or close to that) of the time. Then, once you have that script working, add your JS funcionality to compliment your existing code.
What I do in mine, is I build an extra array, in this case $display_msg
and I run a check of all the input fields. If a field fails, I add that to the array, $display_msg['password']
then I move on to the next field. Once all the fields have been check, I check if the $display_msg
variable is empty
or not. If it's not empty, then I have it fill all the span
next to the input boxes with red letters explaining the error. This will print out ALL of the errors at the same time, instead of one at a time while it works it's way down the form. Next to each input I have a span
with the given variable name, in this instance <?php echo $display_msg['password']?>
, then next to the username, <?php echo $display_msg['username']?>
and so on. Hope this is clear enough and helps.
Upvotes: 0
Reputation: 121
http://jquery.malsup.com/form/
Here's a good jQuery AJAX form plugin. This will prevent page refresh upon submission. Hope this helps.
Upvotes: 0
Reputation: 69957
If you don't want the browser to leave the page when the form is submitted, then you will have to use AJAX to submit the form in the background to communicate with the server and then update the container with the error message (or something different on success).
Alternatively, have the PHP form post to itself, check the error messages before you output your HTML and if there was an error, insert the error message in the form markup in the desired location and re-populate the form with all of the values that were originally submitted.
Upvotes: 1