PHP Tutorial: A n00b’s Guide To Making A Signup Form with PHP and WordPress

If you’re new to web development and want to learn how to create a simiple signup form with PHP and Wordpress, check out this PHP tutorial! By Ray Wenderlich.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Checking User Input

The next step we need to do is check the user input. We need to make sure that the user actually filled in their username and email, and figure out whether they’ve checked the notify checkbox or not.

So replace the checkInput method with the following:

function checkInput() {
				
	// Bail right away if our hidden form value isn't there
	if (!(isset($_POST['my-form-data']))) { return false; }

	// Initialize errors
	$this->testerNameError = false;
	$this->testerEmailError = false;
			
	// Set variables from post
	$this->testerName = $_POST['tester_name'];
	$this->testerEmail = $_POST['tester_email'];
	$this->notify = $_POST['notify'];
			
	// Check tester name
	if (empty($this->testerName)) { $this->testerNameError = true; }
	
	// Check tester email		
	if (empty($this->testerEmail)) { $this->testerEmailError = true; }
			
	// Bail on errors
	if ($this->testerNameError || $this->testerEmailError) return false;		

	if (empty($this->notify)) {
		$this->notifyInt = false;
	} else {
		$this->notifyInt = true;
	}			
			
	return true;
}

Here we first read in what the user has set for tester_name, tester_email, and notify. If either tester_name or tester_email is empty, we set the appropriate error variable and bail. Remember from the earlier code that if these error variables are set, we’ll output some extra HTML that will warn the user that these fields are required.

Finally, HTML has this weird property where if a checkbox isn’t checked, it doesn’t send the post variable at all. So we account for that and set a variable to true or false to indicate whether it was checked or not here as well.

Refresh your web page again and try logging in, but this time leave your name and/or email blank. If all works well, it should present a warning to that effect:

Displaying Validation Error

Inserting the Beta Signup

Now for the moment we’ve been waiting for – actually inserting the user’s signup into our database!

But to do that we need to know is how to access the database. Well, in WordPress provides a global variable that is already set up to talk to the WordPress database called $wpdb. So we can just use that rather than having to connect manually.

And once we know that, the code is rather simple! Add this function to your MyBetaSignup class:

function insertUser() {
	
	global $wpdb;
	
	$wpdb->hide_errors();
	
	$tableName = $wpdb->prefix . "my_beta_user";
					
	return $wpdb->query($wpdb->prepare("
			INSERT INTO ".$tableName."
			(id, name, email, notify)
			VALUES ( 0, %s, %s, %d)",
			$this->testerName, $this->testerEmail, $this->notifyInt));
					
}

Here we create our table name, then prepare and execute our insert statement. Note that we use prepare to create our statement to make sure that the values from the user are escaped.

So just modify the showSignupForm to wrap this up:

function showSignupForm($atts) {
	
	if ($this->checkInput()) {
	
		$success = $this->insertUser();
		
		if ($success) {
			$form = '
				<div class="my-form">
					<h3>Successfully signed up!</h3>
					<p>You have successfully signed up for notification when My Awesome App launches.</p>
					<p>Thank you very much for your interest, and wishing you many exciting adventures ahead!</p>
				</div>';
			return $form;
		} else {
			$form = '
				<div class="my-form">
					<h3>Error signing up :[</h3>
					<p>It seems like there was a problem signing you up for the beta.  Please try again later or contact us to let us know about the problem.  Sorry for the inconvenience!</p>
				</div>';
			return $form;
		}		
	
	} else {
	
		// Rest of code...

And that’s it! Save your plugin, and try out your signup form, and you should be able to sign up successfully!

Successful Beta Signup

Where To Go From Here?

Here is a zip file with the plugin we developed in the above PHP tutorial.

For your beta, you may wish to add additional fields to your database or signup form – you can just extend the above to do so.

And for all of you web dev pros out there – let me know if I messed up with anything here, I’m pretty new to this! :]

Contributors

Over 300 content creators. Join our team.