views:

83

answers:

1

When visitors register on my site, they will do so as the founding member of a group or as a person joining an existing group. Nobody is "group-less." I have a single registration page with the following fields: first name, last name, (radio buttons to choose group type: New or Existing), group name, group password, email, pass, confirm pass. I'm concerned specifically with the radio buttons, group name, and group password.
The specific function I'm looking for is this: If "New" is selected, AJAX checks if group name exists in database, displays either "Good" or "Already taken." ALSO: when form is submitted, password saved to database.
If "Existing" is selected, AJAX checks if group name exists in db, displays either "Matched" or "Non-existent." ALSO: when form is submitted, password checked against database.

The basics of this I think I have a handle on, but how to get the radio button to dictate so much is beyond me. Any help would be appreciated.

Below is the form part of my php file. (By the way, I've been writing mysqli for this project.)

<h1>Register</h1>
<form action="register.php" method="post">
    <fieldset>

    <p><b>First Name:</b> <input type="text" name="first_name" size="20" maxlength="20" value="<?php if (isset($trimmed['first_name'])) echo $trimmed['first_name']; ?>" /></p>

    <p><b>Last Name:</b> <input type="text" name="last_name" size="20" maxlength="40" value="<?php if (isset($trimmed['last_name'])) echo $trimmed['last_name']; ?>" /></p>

  <p><b>Are you registering a new group or joining an existing group?</b> <br />
  New:<input type="radio" value="new" name="gtype">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  
  Existing:<input type="radio" value="existing" name="gtype">  </p>

    <p><b>Group ID:</b> <input type="text" name="group_id" size="20" maxlength="40" value="<?php if (isset($trimmed['group_id'])) echo $trimmed['group_id']; ?>" /></p>

    <p><b>Group Password:</b> <input type="password" name="gpass" size="8" maxlength="5" /> 
      <small>Use only numbers. Must be 5 digits long.</small></p>

    <p><b>Email Address:</b> 
    <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" onBlur='checkEmail(this)'/><span id='info'></span></p>

    <p><b>User Password:</b> 
      <input type="password" name="password1" size="8" maxlength="4" /> 
      <small>Use only numbers. Must be 4 digits long.</small></p>
    <p><b>Confirm Password:</b> <input type="password" name="password2" size="4" maxlength="4" /></p>
    </fieldset>

    <div align="center"><input type="submit" name="submit" value="Register" /></div>
    <input type="hidden" name="submitted" value="TRUE" />

</form>
+1  A: 

I suggest using jquery:

Detect the change of the radio button, like this:

$("input[@name='gtype']").change(

Grab the content of the text input containing the group name, and send it to a PHP script.

$.ajax
        ({
            url: 'Apps/CheckGroupName.php',
            type: 'POST',
            data: 'groupName=' + groupName,

            success: function(result)
            {
                // Do whatever you need to do based on the result
            }
        });

In the PHP script, check the group name in the database, and echo the result. In JQuery, that result will end up in success: function (result), where you can do whatever you need to do (like warn the user, or confirm that the group doesn't exist.

Sylverdrag
I'm sorry. Something is escaping me here. I don't quite understand how/where I should use that first bit of code. Could you explain? Thank you.
David
@David: Ah, you have never used jQuery, have you? You are in for a treat, but you will have to study a bit first to understand how it works. I recommend you go through the excellent video tutorials at http://blog.themeforest.net/screencasts/jquery-for-absolute-beginners-video-series/. You will have to invest a bit of time, but you will be glad you did. If I had to explain the code for you, I would have to explain how to install the library, how to work with the DOM and pretty much everything else which is already covered in these tutorials.
Sylverdrag