I have a simple form that loads volunteers into a database. At the same time that someone signs up to volunteer via this form I would like to sign them up for the Drupal website associated with the project.

What php can I use to register a user? The only data required is "Username" and "Email," how do I get those two pieces of data into the proper user registration function?

Comments

Jerimee’s picture

Should I rephrase it? Am I thinking about the problem the wrong way?

Jerimee’s picture

This is the relevant code at mysite/admin/user/user/create, but I'm not clear on what happens when you post to "user-register." User-register is a function within the Drupal core (probably user module), but how do I access that function from outside Drupal (i.e. my own form script)?

<form action="/admin/user/user/create"  method="post" id="user-register">

<div><input type="hidden" name="destination" id="edit-destination" value="admin/user/user/create"  />
<div class="form-item">
 <label for="edit-name">Username: <span class="form-required" title="This field is required.">*</span></label>
 <input type="text" maxlength="60" name="name" id="edit-name"  size="60" value="" class="form-text required" />
 <div class="description">Your preferred username; punctuation is not allowed except for periods, hyphens, and underscores.</div>
</div>
<div class="form-item">
 <label for="edit-mail">E-mail address: <span class="form-required" title="This field is required.">*</span></label>

 <input type="text" maxlength="64" name="mail" id="edit-mail"  size="60" value="" class="form-text required" />
 <div class="description">A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.</div>
</div>
<div class="form-item">
 <div class="form-item">
 <label for="edit-pass-pass1">Password: <span class="form-required" title="This field is required.">*</span></label>
 <input type="password" name="pass[pass1]" id="edit-pass-pass1"  size="25"  class="form-text required" />
</div>
<div class="form-item">

 <label for="edit-pass-pass2">Confirm password: <span class="form-required" title="This field is required.">*</span></label>
 <input type="password" name="pass[pass2]" id="edit-pass-pass2"  size="25"  class="form-text required" />
</div>

 <div class="description">Provide a password for the new account in both fields.</div>
</div>
<div class="form-item">
 <label>Status: </label>
 <div class="form-radios"><div class="form-item">

 <label class="option"><input type="radio" name="status" value="0"   class="form-radio" /> Blocked</label>
</div>
<div class="form-item">
 <label class="option"><input type="radio" name="status" value="1"  checked="checked"  class="form-radio" /> Active</label>
</div>
</div>
</div>
<div class="form-item">
 <label class="option"><input type="checkbox" name="notify" id="edit-notify" value="1"   class="form-checkbox" /> Notify user of new account</label>

</div>
<input type="hidden" name="form_token" id="edit-user-register-form-token" value="e6eeeee17eafd4dd32f9a533332916d5"  />
<input type="hidden" name="form_id" id="edit-user-register" value="user_register"  />
<input type="submit" name="op" id="edit-submit" value="Create new account"  class="form-submit" />

</div></form>
gpk’s picture

I can see 2 ways of approaching this:

1. Convert your custom PHP page/form into a page/node on the Drupal site. You would then need to use Drupal's Forms API to create and process the form (see the relevant links here http://api.drupal.org/api/5). Actually you might need to do this in a custom module http://drupal.org/node/508. I'm not sure whether you can get forms to work properly if they are embedded in a page.

2. Include the following at a relevant point in your PHP file:

require_once './includes/bootstrap.inc'; // modify the path as necessary
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

This gives you full access to Drupal from your script. You could then call user_register() having populated $form_id and $form_values appropriately. You could even call http://api.drupal.org/api/function/user_save/5 directly, but be aware that this could be open to abuse as it would bypass security checks etc. Also if the username or email address is not unique then you may get various errors.

Alternatively Drupal does support distributed authentication so that might be another way to access the account creation functionality. Actually, that's probably a much better way of doing it! OpenID is in Drupal 6 core http://drupal.org/node/152893, and available as a module in Drupal 5 http://drupal.org/project/openid.

gpk
----
www.alexoria.co.uk

Jerimee’s picture

I really needed the help. Now I can get back to work.

While I am distrustful of things I do not understand, I'll try OpenID since you recommend it. I'm using Drupal 5, so hopefully the module aint' buggy.

If I run into a wall on OpenID, I'll try including the drupal_bootstrap. Thank you for showing me how to do this.

Jerimee’s picture

I tried OpenID as a solution, but after playing with it a bit I decided I don't have enough familiarity with (or trust in) OpenID to use that as the solution.

It just doesn't seem user friendly enough. It seems to require people to login twice (eliminating the convenience), and a duplicate username causes it to fail.

I'm going to try the drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL) method now.