Solved

Form Posting Within OO Class

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mail Not Sent 6 42
PHP Startup Error 12 30
php variable basic question 12 27
How to display a functions data inside HEREDOC 2 14
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

13 Experts available now in Live!

Get 1:1 Help Now