Solved

Form Posting Within OO Class

Posted on 2014-10-15
2
113 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 109

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cannot call a static method on an Eloquent object 6 29
PHP AJAX Wordpress 9 19
Really simple no curl. Send a post 3 times 4 22
two ways encryption with php 3 24
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…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

816 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

12 Experts available now in Live!

Get 1:1 Help Now