Solved

Form Posting Within OO Class

Posted on 2014-10-15
2
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
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 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 …

739 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