Permalink
Browse files

response saves

  • Loading branch information...
1 parent e209f0c commit 986b57e796ce94f2593987e680b02c2a0a7f2d30 @viveleroi committed Dec 15, 2011
Showing with 55 additions and 7 deletions.
  1. +4 −4 Formbuilder/Formbuilder.php
  2. +17 −0 Formbuilder/Formbuilder_pdo.php
  3. +8 −1 Formbuilder/sql/mysql.sql
  4. +1 −0 README
  5. +25 −2 example-submit.php
View
8 Formbuilder/Formbuilder.php
@@ -135,7 +135,7 @@ public function generate_html($form_action = false){
*/
public function process(){
- $error = '';
+ $error = array();
$results = array();
// Put together an array of all expected indices
@@ -151,7 +151,7 @@ public function process(){
$val = $this->getPostValue( $this->elemId($field['values']));
if($field['required'] && empty($val)){
- $error .= '<li>Please complete the ' . $field['values'] . ' field.</li>' . "\n";
+ $error[] = 'Please complete the ' . $field['values'] . ' field.';
} else {
$results[ $this->elemId($field['values']) ] = $val;
}
@@ -161,7 +161,7 @@ public function process(){
$val = $this->getPostValue( $this->elemId($field['title']));
if($field['required'] && empty($val)){
- $error .= '<li>Please complete the ' . $field['title'] . ' field.</li>' . "\n";
+ $error[] = 'Please complete the ' . $field['title'] . ' field.';
} else {
$results[ $this->elemId($field['title']) ] = $val;
}
@@ -186,7 +186,7 @@ public function process(){
}
if(!$at_least_one_checked && $field['required']){
- $error .= '<li>Please check at least one ' . $field['title'] . ' choice.</li>' . "\n";
+ $error[] = 'Please check at least one ' . $field['title'] . ' choice.';
}
}
} else { }
View
17 Formbuilder/Formbuilder_pdo.php
@@ -101,6 +101,23 @@ public function render_html( $form_db_id = false, $form_action = "" ){
/**
+ *
+ * @param type $form_db_id
+ * @return type
+ */
+ public function save_response( $form_db_id = false ){
+ $results = $this->process($form_db_id);
+ if($results['success']){
+ $stmt = $this->_db->prepare("INSERT INTO fb_savedresponses (response_json,date_created) VALUES (:json,:date)");
+ $stmt->bindParam(':json', json_encode($results['results']), PDO::PARAM_STR);
+ $stmt->bindParam(':date', date("Y-m-d H:i:s"), PDO::PARAM_STR);
+ $stmt->execute();
+ }
+ return $results;
+ }
+
+
+ /**
*
* @param type $form_db_id
* @param type $form_action
View
9 Formbuilder/sql/mysql.sql
@@ -2,4 +2,11 @@ CREATE TABLE IF NOT EXISTS `fb_savedforms` (
`id` int(11) NOT NULL auto_increment,
`form_structure` longtext NOT NULL,
PRIMARY KEY (`id`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
+
+CREATE TABLE IF NOT EXISTS `fb_savedresponses` (
+ `id` int(10) unsigned NOT NULL auto_increment,
+ `response_json` text NOT NULL,
+ `date_created` datetime NOT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
View
1 README
@@ -25,6 +25,7 @@ Version 0.4 - 20111215
- Adding example mysql storage methods to the load, save process (PDO-based so other databases may be used)
- Incorporated a required attribute bug fix: https://snowy-evening.com/botsko/jquery-form-builder/8/
- Dumped a ton of unnecessary complexity of encoding/decoding and array vars
+- Changed process() to return an array of errors, not a string
Version 0.3.1 - 20110722
- Corrected issue with reserved words breaking support for Safari, Opera, IE
View
27 example-submit.php
@@ -19,17 +19,40 @@
//$form = new Formbuilder($fake_db_vals);
//$results = $form->process();
-// OR to process a form saved to the db:
+// OR you can process form based on the one stored in the db
require('Formbuilder/Formbuilder_pdo.php');
$form = new Formbuilder_pdo();
$form->connect();
-$results = $form->process(1);
+$results = $form->save_response(1);
+
+// At this point though, there are a few choices to save this to a DB
+// It's harder to store the values because the fields change and are more
+// flexible than normal. You can hard-code a structure of a db,
+// and override the save_response method to save the field data
+// to a table, one field per column. This makes it much easier to query against
+// but it makes it possible for the db and the form to loose sync.
+
+// In this example method we're only saving the json encoded response but this
+// makes it a lot harder to query against. I'd recommend using something more
+// appropriate for this JSON structure, like MongoDB. Anyway, good luck!
+
+
+
print '<pre>';
var_dump($results);
print '</pre>';
+
+// Here is an example printing the errors
+if(!empty($results['errors'])){
+ print '<ul>';
+ foreach($results['errors'] as $err){
+ printf("<li>%s</li>\n", $err);
+ }
+ print '</ul>';
+}
?>
</body>

0 comments on commit 986b57e

Please sign in to comment.