9 Replies - 792 Views - Last Post: 06 January 2013 - 02:36 PM Rate Topic: -----

#1 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 227
  • Joined: 31-March 10

How do you guys handle form data?

Posted 04 January 2013 - 05:54 AM

Hi,

I am quite intrigued by how different PHP developers handle form data that goes into a database.

Do you handle the form data individually and use IF statements to ensure they are valid etc?

Do you manually write the SQL INSERT/UPDATE and PDO queries?

I'm not trying to "steal" ideas or methods, I just simply want to know how different people approach this process.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: How do you guys handle form data?

#2 Faitas  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 44
  • Joined: 04-November 11

Re: How do you guys handle form data?

Posted 04 January 2013 - 06:16 AM

Hello, Ryan,

I usually use CodeIgniter PHP framework, so I can type some lines and framework validate it itself. (I use form validation class).
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: How do you guys handle form data?

Posted 04 January 2013 - 07:20 AM

generally, I use PHP’s filter functions and Prepared Statements for DB queries.
Was This Post Helpful? 0
  • +
  • -

#4 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 227
  • Joined: 31-March 10

Re: How do you guys handle form data?

Posted 04 January 2013 - 09:11 AM

I am aware of the various validation techniques but do you actually hand type the validation code for each field submitted?

Example.

if($_POST['name'] != '') {
   $name = strtoupper($_POST['name']);
}

if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
   $email = $_POST['email'];
}
...



The same goes for the SQL string. Do you actually hand type the prepared statement?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: How do you guys handle form data?

Posted 04 January 2013 - 10:22 AM

View PostRyanRobinson, on 04 January 2013 - 05:11 PM, said:

Do you actually hand type the prepared statement?

no. one feat of OOP is re-useability.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3069
  • View blog
  • Posts: 10,751
  • Joined: 08-August 08

Re: How do you guys handle form data?

Posted 04 January 2013 - 12:24 PM

Here's something you might find helpful:
<?php
$myform = <<<HERE
<form method="post">
Name: <input type="text" name="name" value="{name}"><br>
Address: <input type="text" name="address" value="{address}"><br>
Phone: <input type="text" name="phone" value="{phone}"><br>
<input type="submit" value="submit">
</form>
HERE;

class validation {
	protected $name;
	protected $address;
	protected $phone;
	protected $expected = array("name", "address", "phone");
	
	function __construct($arr) {
		$this->infos = array();
		foreach($this->expected as $key) {
			if(!empty($arr[$key])) {
				$this->$key = $arr[$key];
			} else {
				$this->$key = "";
			}
			$this->infos[] = $this->$key;
		}
	}
	
	function get_all() {
		$data = array();
		foreach($this->expected as $key) {
			$data[] = $this->$key;
		}
		return $data;
	}
	
	function validate_all() {
		$errors = array();
		foreach($this->expected as $key) {
			$func = "validate_".$key;
			if(!$this->$func()) {
				echo "???<br>";
				$errors[] = 1;
			}
		}
		return $errors;
	}
	
	function validate_name() {
		return true; // Replace with validation code for this property
	}
	
	function validate_address() {
		return true; // Replace with validation code for this property
	}
	
	function validate_phone() {
		return true; // Replace with validation code for this property
	}
	
	function save_info($pdo) {
		// Code to save in database.
	}
}

$form_names = array("name", "address", "phone");
$form_types = array("text","text","phone");
$form_values = new validation($_POST);

$errors = $form_values->validate_all();
if(count($errors) > 0) {
	echo "The form did not validate properly<br>";
}

$replace = array("{name}", "{address}", "{phone}");
$output = str_replace($replace, $form_values->get_all(), $myform);

echo $output;


This post has been edited by CTphpnwb: 04 January 2013 - 12:25 PM

Was This Post Helpful? 3
  • +
  • -

#7 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 227
  • Joined: 31-March 10

Re: How do you guys handle form data?

Posted 06 January 2013 - 04:28 AM

Hmmm some good techniques here. However, do any of you get sick of hand cranking this code for each field? I've been playing around with some code recently and I have managed to put together some really neat code. I have looked around quite extensively to find other examples of what I'm trying to do but couldn't find anything. I was quite surprised that there is a lot devs still writing a lot of code.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3069
  • View blog
  • Posts: 10,751
  • Joined: 08-August 08

Re: How do you guys handle form data?

Posted 06 January 2013 - 06:22 AM

A class like this could be stored in its own file and included in projects with little or no modification. There's no need to hand crank anything.
Was This Post Helpful? 0
  • +
  • -

#9 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 227
  • Joined: 31-March 10

Re: How do you guys handle form data?

Posted 06 January 2013 - 01:12 PM

Yes I know that you can just include that class. But it means adding more protected properties and array items for each new field you want to process. Depending on the project, your class could just keep growing and will be harder to maintain.
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3069
  • View blog
  • Posts: 10,751
  • Joined: 08-August 08

Re: How do you guys handle form data?

Posted 06 January 2013 - 02:36 PM

No matter what you do you'll need at least another function for each type of field you want to validate.

You could make it a little simpler by making the fields be a protected array in the class, but it doesn't seem like you'd gain much and I thought it would have clouded the point I was trying to make so I didn't do it in my example.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1