PDA

View Full Version : php form submitting



bberry91
14 Mar 2011, 08:03 AM
I have a php form, and have about 15 fields. Right now, I am submitting the fields to the form_send.php as:


$name = $_POST['name'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$phone1 = $_POST['phone1'];
$phone2 = $_POST['phone2'];
$phone3 = $_POST['phone3'];
$business = $_POST['business'];

What would be a quicker, more efficient way? Would I use arrays, or something of the sort? Or can I get each field, and set it to the form field name, so I only need one line of code?

Thanks.

is_numeric
14 Mar 2011, 08:53 AM
The contents of a posted form is stored in a predefined variable $_POST (Its data is stored as an array)

Although its not required you should assign the values to a variable so you can perform some validation process on the values before processing further. I mentioned its not required as you can use the $_POST directly in place of the variable but I prefer to lay out my POSTS as you have done when coding procedurally.

The other way would be to create a method in a class (OOP) which processes the POSTED data. You will still have to pass in the $_POST array but you could automate some the assignment and then reuse the class in other projects.

For example you could loop through the $_POST array and using Variable Variable names...


foreach($_POST as $key=>$value){

$$key = $value;

}

NOTE: The double $$. This creates a variable from your KEY names in this example.

To summarise your not doing anything wrong.

There is always more than one way to skin a cat and each has their own merits