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.
How do you guys handle form data?
Page 1 of 19 Replies - 326 Views - Last Post: 06 January 2013 - 02:36 PM
Replies To: How do you guys handle form data?
#2
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).
I usually use CodeIgniter PHP framework, so I can type some lines and framework validate it itself. (I use form validation class).
#3
Re: How do you guys handle form data?
Posted 04 January 2013 - 07:20 AM
#4
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.
The same goes for the SQL string. Do you actually hand type the prepared statement?
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?
#5
Re: How do you guys handle form data?
Posted 04 January 2013 - 10:22 AM
#6
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
#7
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.
#8
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.
#9
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.
#10
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|