foreach loop for live form validation
<p>I am trying to validate a form using jQuery and PHP that will check to see if:
a) the input is valid (valid email, etc)
b) the username or email address already exists</p>

<p>Here is the JavaScript code on the <strong>register.php</strong> page:</p>

<pre><code>&lt;script language="javascript"&gt;
    $(document).ready(function()
{
    $("#wl-btn").click(function()
    {
    $("#msgbox").removeClass().addClass('messagebox').html('&lt;div class="msg-info"&gt; Validating form...&lt;/div&gt;').fadeIn(1000);
    $.post("check_details.php",{ username:$('#wl-username').val(),password:$('#wl-password').val(),password2:$('#wl-password2').val(),email:$('#wl-email').val(),type:$('#wl-type').val(),rand:Math.random() } ,function(data)
    {
//////////// THIS IS THE CODE THAT I NEED TO ALTER TO CATER FOR CHECKING THE USERNAME AND EMAIL ADDRESS
        if(data=='userexists')
        {
        $("#msgbox").fadeTo(200,0.1,function()
        { 
            $(this).html('&lt;div class="msg-error"&gt;That username has been taken! Please try another username.&lt;/div&gt;').addClass('messageboxerror').fadeTo(900,1); 
        });
                    $("#wl-username").fadeTo(200,0.1,function()
                    {
                        $(this).addClass('input-error tiptip-bottom').fadeTo(900,1);
                    });
                    $("#wl-username-text").fadeTo(200,0.1,function()
                    {
                       $(this).text('Please choose a different username!').addClass('msg-form-error').fadeTo(900,1); 
                    });
////////////////////////////////////////
                } else if(data=='yes'){
                    $("#msgbox").fadeTo(200,0.1,function()
        { 
            $(this).html('&lt;div class="msg-ok"&gt;Registration successful! Please check your email inbox for instructions on how to activate your account...&lt;/div&gt;').addClass('messageboxok').fadeTo(900,1); 
        });
        } else {
        $("#msgbox").fadeTo(200,0.1,function()
        { 
            $(this).html('&lt;div class="msg-error"&gt;Generic form error message.&lt;/div&gt;').addClass('messageboxerror').fadeTo(900,1);
        });     
        }           
    });
    return false;
    });
});
&lt;/script&gt;
</code></pre>

<p>As you can see, it is calling <strong>check_details.php</strong> and passing the POST values to that script. At the moment I have got this snippet of code to check if the user exists:</p>

<pre><code>&lt;?php

$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$count = mysql_num_rows($query);
if ($count &gt; 0) {
    echo "userexists";
}

?&gt;
</code></pre>

<p>I need to find a way to validate the user input and check if the username and email address already exists in the database.</p>

<p>If the user input is in an incorrect format or the username/email address exists then I need the JavaScript code to add the appropriate error classes and messages (as seen in the first part of the JS code above).</p>
