Solved

Form Posting Within OO Class

Posted on 2014-10-15
2
112 Views
Last Modified: 2014-11-25
I know this question can have multiple answers based on a users preference, but is it good practice to put form post variables within a class and do the database inserts all from within the class, or on the actual web page?

Say I have the following scenario, where I have a form that asks a user for a first name, last name, and e-mail address.  The form post would look like this:

<?php

if(isset($_POST['submitform'])) {
	if(empty($_POST['firstname'])) { 
		$Errors[] = 'You must enter a first name!';
	} else {
		$firstname = sanitizeFunction($_POST['firstname']);
	}

	if(empty($_POST['lastname'])) { 
		$Errors[] = 'You must enter a last name';
	} else {
		$lastname = sanitizeFunction($_POST['lastname']);
	}

	if(empty($_POST['email'])) { 
		$Errors[] = 'You must enter an e-mail address!';
	} else {
		$email = sanitizeFunction($_POST['email']);
	}
	
	if(!isset($Errors)) {
		//Form already validated and cleansed - No Errors
		$ins = mysqli_query($link, "INSERT INTO mytable (`firstname`, `lastname`, `email`) VALUES ('".$firstname."', '".$lastname."', '".$email."')") or die("Cannot complete query!");
		
		if($ins) {
			location("successpage.php");
		}
	}
}

Open in new window


Is this acceptable practice for object oriented, or would it be more wise to create a method within my class to do the database insert there. If it's the latter, what should the method look like based on the given code above?
0
Comment
Question by:t3chguy
2 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 40382912
It really doesn't matter.  The reason I say that goes to the nature of object-oriented design.  We use OO techniques to make our code modular and reusable.  PHP has already buggered the modularity by making $_POST a mutable superglobal variable.  It's present in every scope and namespace.  And if you're going to reuse this piece of the script, perhaps by autoloading it, your application may be misdesigned (I'm thinking that a form for contact information should be in one and only one place in the application).  So it's kind of a thing that doesn't fit very well into the OO concept.

If you're already using a database abstraction layer you might extend either MySQLi or PDO to add database specific code, conceptually something like a method $db->put_user_data().  But I wouldn't obsess too much about it.  Either way is likely to work out well for you.
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 250 total points
ID: 40385178
I use a class to handle forms and the data is gathered externally into a "vars" array which contains the data in an array keyed by HTML field name. This is then passed to the form via a method

$form->setDataSet( $varsArray );

Things like database inserts are handled by a completely separate class because a class should only ever have one job. Either it handles a form or it maintains a table
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now