Showing message from php action script in form
<p>How do I show a message from the php action script of a form?</p>

<p>The form is a user login with these fields.</p>

<pre><code>&lt;div id="register_user_box" class="inline_form" style="position: absolute; top: 20px; right: 10px; &lt;br/&gt;
     &lt;span id="user_msg"&gt;&lt;/span&gt;&lt;br/&gt;
     &lt;form action="register_user.php" method="post"&gt; 
     &lt;input type="hidden" name="id" id="id" value="add" /&gt; 
     &lt;input type="hidden" name="edit_user" id="edit_user" value="y" /&gt;
     &lt;table cellspacing="0px"&gt; &lt;tr&gt;&lt;td&gt;Username:&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;
     &lt;input type="text" name="uname" size="30" value="" class="inline_input"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Email:&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;
     &lt;input type="text" name="uemail" size="30" value="" class="inline_input"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Password:&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;
     &lt;input type="password" name="upass" size="30" class="inline_input"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Confirm Password:&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt; &lt;td&gt; 
     &lt;input type="password" name="cpass" size="30" class="inline_input"/&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;p&gt;
     &lt;input class="button" type="submit" name="register" value="Register" style="float:right;"/&gt;&lt;/p&gt;     
     &lt;/form&gt;
&lt;/div&gt;
</code></pre>

<p>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.</p>

<pre><code>&lt;?php

    $messages = array( 'usr_cred_req' =&gt; 'Must specify username, email, password.',
                        'usr_name_bad' =&gt; 'Bad username selection.  Select a different usrename.', 
                        'usr_name_exists' =&gt; 'Username selected already exists.  Select a different username.',
                        'usr_email_bad' =&gt; 'Bad email selection.  Select a different email.',
                        'usr_email_exists' =&gt; 'Email selected already exists.  Select a different email.',
                        'usr_pass_notmached' =&gt; 'Passwords do not match.  Type passwords again.', 
                        'usr_not_added' =&gt; 'User not added.',
                        'usr_not_updated' =&gt; 'User not updated.',
                        'usr_added' =&gt; 'User added.'
                        );

    $username = trim($_REQUEST['uname']);
    $email = trim($_REQUEST['uemail']);
    $password = md5(trim($_REQUEST['upass']));
    $copasswd = md5(trim($_REQUEST['cpass']));


    if ( $password != $copasswd ) { echo '&lt;script&gt; $("#usr_msg").html("'.$messages['usr_pass_notmached'].'"); &lt;/script&gt;'; return;}
?&gt;
</code></pre>

<p>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.</p>
