I’ve got checkboxes in my form. When it saves the form data into the DB it just prints the word "ARRAY". How do you save the array of info from a form with checkboxes?
<?php
function eForm2db( &$fields )
{
/*---------------------------------------------------------------
eForm2db
Version: 0.1 [Beta 1]
Author: pixelchutes
---------------------------------------------------------------
Requirements:
eForm 1.4+
---------------------------------------------------------------
Use:
- Takes validated eForm submittal data for use with the MODx DBAPI extender class.
- Easily process database records referencing your form submission data:
+ INSERT new records to a table of your choice
+ UPDATE existing records if you'd prefer
+ DELETE records based on form submittal criteria
+ Form a query and return its record set as XML using $modx->db->getXML
+ Query a table's MetaData using $modx->db->getTableMetaData, followed by an update query
on only the form fields that are named after table columns!
+ and much more!
---------------------------------------------------------------*/
// Bring needed resources into scope
global $modx, $table_prefix;
// Init our array
$dbTable = array(); // key = DB Column; Value = Insert/Update value
$dbTable['name'] = $fields['name'];
$dbTable['name2'] = $fields['name2'];
$dbTable['email'] = $fields['email'];
$dbTable['comments'] = $fields['comments'];
$dbTable['mailing'] = $fields['mailing'];
// Insert field/value pairs to insert/update in our table
//$dbTable[name] = $fields[first_name].' '.$fields[last_name]; // Merge two form fields together
//$dbTable[datetime] = date( 'YmdHis', strtotime( $fields[postdate] ) ); // Massage the postdate timestamp to be MySQL insert friendly
INSERT - $dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'customers' );
// UPDATE - $dbQuery = $modx->db->update( $dbTable, $table_prefix . 'updateTableName' );
// DELETE - $dbQuery = $modx->db->delete( $table_prefix . 'deleteFromTableName', 'some_field = 1 AND name=\''.$dbTable[name].'\'', '' );
// etc...
// Run the db insert query
$dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'customers' );
return true;
}
// Return empty string
return $sql;
?>$ds = $modx->db->select('*','william_data.customers');
$ds = $modx->db->select('*','william_data.modx_customers');
Having looked at the DBAPI instructions it looks like I ougt to be able to insertor$ds = $modx->db->select('*','william_data.customers');to get this to put the data into the alternative database. However, I am not sure if this code is correct or exactly where to put it, or if there is an additional change I need to make. All experiments so far have resulted in errors or blank pages.$ds = $modx->db->select('*','william_data.modx_customers');
If anyone can help I would be very grateful. Thanks, Andy
$ds = $modx->db->select('*','william_modx.modx_customers');
$ds = $modx->db->select('*','william_data.modx_customers');
$ds = $modx->db->select('*','william_data.customers');$ds = $modx->db->select('*','william_data.modx_customers');$ds = $modx->db->select('*','william_data.customers');$ds = $modx->db->select('*','william_data.modx_customers');$modx->db (the default DB)
$myDb = new DBAPI('12.23.4.5','mydb', 'username','password', 'myprefix_', '[charset]');// Bring needed resources into scope
$ds = $modx->db->select('*','william_data.modx_customers');
global $modx, $table_prefix;Fatal error: Call to a member function select() on a non-object in /home/william/www/dev/manager/includes/document.parser.class.inc.php(770) : eval()'d code on line 23
$this->loadedjscripts= array ();
// constructor
function DocumentParser() {
$this->loadExtension('DBAPI') or die('Could not load DBAPI class.'); // load DBAPI class
$this->dbConfig= & $this->db->config; // alias for backward compatibility
$this->jscripts= array ();
$this->sjscripts= array ();
$this->loadedjscripts= array ();
// events
$this->event= new SystemEvent();
$this->Event= & $this->event; //alias for backward compatibility
$this->pluginEvent= array ();
// set track_errors ini variable
@ ini_set("track_errors", "1"); // enable error tracking in $php_errormsg
}
I’ve been doing some cool stuff with eform2db to save data from a form, and also load it into the form, perhaps these examples will help you:
CREATE TABLE `modx_rsvp_venues` ( `id` int(10) unsigned NOT NULL auto_increment, `member_id` varchar(50) NOT NULL, `venue_address` varchar(255) NOT NULL, `venue_postcode` varchar(12) NOT NULL, `venue_time` varchar(40) NOT NULL, PRIMARY KEY (`id`) ) CREATE TABLE `modx_rsvp_events` ( `venue_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `member_id` varchar(50) NOT NULL, PRIMARY KEY (`venue_id`) )
[[populateUserForm]] [[Users2db]] [[eForm?formid=`user_form` &protectSubmit=`0` &tpl=`user_form` &noemail=`1` &eFormOnBeforeMailSent=`Users2db` &eFormOnBeforeFormParse=`populateUserForm`]]
<?php
function populateUserForm(&$fields, &$templates) {
global $modx;
$userinternalkey = $_SESSION['mgrInternalKey'];
//$userform_id = $_GET['id'];
$evt =$modx->event; // The Snippet call event (not eFormOnBeforeFormParse event)
// Replicate $isPostBack variable
$validFormId = ($evt->params['add_user'] == $_POST['add_user']);
$isPostBack = ($validFormId && count($_POST) > 0);
// Some tests
echo "userinternalkey=".$userinternalkey."<br>";
echo "submit set=".isset($_POST['submit'])."<br>";
echo "validFormId=".$validFormId."<br>";
echo "count post =".count($_POST)."<br>";
echo "is PostBack=".$isPostBack."<br>";
//select uit db
if (!$isPostBack) {
echo "no postback";
// Check for NOT postback, cuz we want to populate an empty form
// Also, make sure we have an id greater than 0
$rs = $modx->db->select('`id`,`internalKey`, `fullname`, `email`, `phone`','modx_user_attributes','internalKey="'. $userinternalkey. '"');
$row = $modx->db->getRow($rs); // Get the single row
if (!empty($row)) // Does the row have data?
$fields[userform_id] = $row[id];
$fields[fullname] = $row[fullname]; // Copy the data over to the fields variable
$fields[email] = $row[email];
$fields[phone] = $row[phone];
} else {
echo "postback";
// The form has beed posted, let's not disturb the data.
}
return true; // A value of false will stop the eForm parser
}
?><?php
function Users2db( &$fields )
{
/*---------------------------------------------------------------
eForm2db
Version: 0.1 [Beta 1]
Author: pixelchutes */
// Bring needed resources into scope
global $modx, $table_prefix;
$usernameid = $_SESSION['mgrInternalKey'];
$vid = $fields[userform_id];
// Init our array
$dbTable = array(); // key = DB Column; Value = Insert/Update value
//$dbTable[id] = $fields[id];
//$dbTable[internalKey] = $usernameid;
$dbTable[fullname] = $fields[fullname];
$dbTable[email] = $fields[email];
$dbTable[phone] = $fields[phone];
if ($vid != "") {
$dbQuery = $modx->db->update($dbTable, 'modx_user_attributes', 'internalKey = " ' . $usernameid .' " ' );
return true;
} else {
$dbQuery = $modx->db->insert( $dbTable, 'modx_user_attributes' );
return true;
}
}
?><span style="color:#900;">[+validationmessage+]</span> <br> <form method="post" name="user_form" id="user_form" class="cssform" action="[~[*id*]~]"> <p> <label for="fullname">Full name:</label> <input type="text" name="fullname" id="fullname" eform="fullname:string:1" value="[+fullname+]" size="20" maxlength="30" /> </p> <p> <label for="venue_address"> Email:</label> <input type="text"name="email" value="[+email+]" id="email" eform="email:email:1" size="20" maxlength="30" /> </p> <p> <label for="phone"> Phone:</label> <input type="text" name="phone" id="phone" value="[+phone+]" eform="phone:string:1" size="20" /> </p> <input type="text"style="display:none;" id="userform_id" name="userform_id" value="[+userform_id+]" eform="userform_id::0"/> <p> <input type="submit" name="submit" class="button" value="Gegevens wijzigen" /> <br> </fieldset> </p> </form>
$validFormId = ($evt->params['add_user'] == $_POST['add_user']);