Magento, PHP, MySQL, AJAX, Zend, cPanel or OpenSource…
how to capture all fields and values of a form at once in php?
Sometime we may have many fields in a form. We can get all field names and corresponding values at once in php nicely and field names which can be used as variables later.
For eg, you would like to save into database then create a table and make all database filed names same as form’s field names.
CREATE TABLE `student` ( `id` INT NOT NULL AUTO_INCREMENT , `first_name` INT NOT NULL , `last_name` INT NOT NULL , PRIMARY KEY ( `id` ) )
I have a form as given below:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> First Name : <input type="text" name="first_name"><br /> Last Name : <input type="text" name="last_name"><br /> <input type="submit" name="submit" value="Submit" > </form>
While you submit the form, capture all variables, values and create SQL query to save into database:
if(!empty($_POST)){
foreach($_POST as $variable => $key){
${$variable} = trim($key);
}
$sql = "INSERT INTO `student` (`first_name` ,`last_name`)
VALUES ( '".$first_name."', '".$last_name."');"
}
| This entry was posted by admin on September 6, 2010 at 1:31 pm, and is filed under General, PHP/MySQL. Follow any responses to this post through RSS 2.0. You can skip to the end and leave a response. Pinging is currently not allowed. |