RSFormsPro is so powerful, you can use it to totally customise Joomla to make your site and components work better for your workflows, business and site. Here, I'm going to describe how, with a little bit of scripting, you can quickly and automatically initialise a set of variables for every one of your form fields with which to operate on. This will save you loads of time if youhave loads of fields you need to insert into a custom table by giving your variables the same names as your fields.
SCENARIO
You have a form set up and are using mappings to insert a new row of data in a mysql table. Your form and mappings are all working fine but now, you want to do a little more $_POST variable processing to 'automate' a few more tasks. The mappings plugin in excellent for straight inserts where you simply want to take a selection or all of a form's data and INSERT it into a new row of a custom or existing component table.
THE POWER OF THE MAPPING PLUGIN
Using the mappings plugin, you can easily integrate any native/3rd-party components by creating custom 'join' tables. This means that it's a perfect tool to effectively build your own component or add your own custom tables to do anything from extending permission sets, adding group permissions, integrate access to components based on your custom information etc.
For instance, you want to relate your employee/customer information to your forum component & certain sections and categories in content in order to give extra permissions to employees from certain departments to manage a forum/news and articles for every new project he/she creates. Likewise, you want the customer to get read access to these.
You can easily achieve this by creating fields (RSForm components) and populating their default values/options with simple MYSQL in PHP. Using the mapping plugin to point your form elements at the fields/tables in the DB, your inserts are handled completely by the plugin.
The beauty of it is that, whatever your customisations/integrations, they can (and should) remain NON-INVASIVE as long as the tables you use to 'join' with your joomla tables/components are custom tables, leaving your installation and components un-tarnished!
NOTE: ONLY USE MAPPINGS & CUSTOM SCRIPTING TO INSERT/UPDATE YOUR DATABASE
IF YOU KNOW WHAT YOU ARE DOING!!!
WHEN THE MAPPINGS PLUGIN MIGHT NOT SUFFICE
Well, there are lots of scenarios in which you may want to be able to operate on $_POST data and the mappings plugin will either be trickier than need be or not possible. Here are a few examples...
Where you wish to:
1 - Insert some rows/data into other columns and you want to feedback/insert that 'other' row's Primary Key into your initial table as a Foreign Key
2 - Update information in other tables whilst you have the $_POST data available
3 - Do some custom validation, code or statistic generation to insert with your initial table
WHY CUSTOM SCRIPTING? MAPPING WAS SO EASY TO USE
I recently discovered that you can't actually achieve this with the mappings plugin - or, at least not very simply. This is because the 'current' $_POST data from the form won't yet have been inserted by the mappings plugin.
FEAR NOT, THE SOLUTION IS QUICK & SIMPLE
If the mapping plugin isn't going to the job for you then we have to create a variable out of everything in the $_POST variable. If you've got a lot of fields like me, you're not going to want to write out the following for each of them:
$myFieldVar = $_POST['form']['myField'];
So how can we achieve a customised set of inserts and updates with minimal scripting?
We are going to create a variable for every field in your form
First, let's check what's in the $_POST array by adding the following to the script called on form process area:
print_r($_POST['form']);
die();
//This will give you a human readable array of the data in you just posted in your form
Now you can see the key=>value pairs and arrays contained in $_POST, you'll see that all of the keys are your field names. So now, using the variable variables technique inside a loop, we can;
- create a variable for each field (key in the array) named the same as your field
- add the field's value(s) to the newly created variable
/*
// loop through the post array //
// $key is the name/id of your field //
// $value is the value of your field //
*/
foreach($_POST['form'] as $key=>$value){
$k = $key; // $k is now =to the id of the field, for example email
$$k = $value; // $$k sets your new variable, i.e. $email, to the value of email on the form
}
This means that now, to refer to the values of your input to the form, you can simply use the dollar sign prepended to the field id. For example:
// Supposing you have a field with id of 'email', you can go ahead and use $email in your script
echo $email; // OR
die($email); // TO CHECK IT'S WORKING
// Now, you're inserts/updates can be written easily and consistently.
DEBUG EASILY - A small if(
OK, so now you've checked everything in the POST variable and you can see all of the $_POST, you wanna check that all of the $elements equivalents exist without echoing them all manually, right? Especially if you have a lot of fields.
Simply REPLACE
// LOOP THE FORM DATA TO SET VARS
foreach($_POST['form'] as $key=>$value){ // loop thru ur post array
$k = $key; // set a variable to the 'name' of your key - $k now = your field id/name
$$k = $value; // Yes, 2 dollar signs - this sets a variable with your fields name to its value in the post array
} // END LOOP THROUGH THE FORM KEY=>VALUES
// You might want to add die statement to prevent any emails and other actions being triggered whilst you debug. Uncomment Below
// die();
WITH
// LOOP THE FORM DATA TO SET VARS
foreach($_POST['form'] as $key=>$value){ // loop thru ur post array
$k = $key; // set a variable to the 'name' of your key - $k now = your field id/name
$$k = $value; // Yes, 2 dollar signs - this sets a variable with your fields name to its value in the post array
// DEBUGGING - comment this block (down to and including "}// END DEBUG" lines
//This will echo/print everything to screen so you can check a variable has been created for all of your fields
if (!is_array($value)){
echo "$$k = ";
//echo just the value
echo "$value<br>";
}else{ // Otherwise print the key name like $myVar and print the array
echo "$".$key." = ";
print_r($value);
echo "<br>";
}// END DEBUG
} // END LOOP THROUGH THE FORM KEY=>VALUES
// You might want to add die statement to prevent any emails and other actions being triggered whilst you debug. Uncomment Below
// die();
AND comment it out when LIVE like:
// LOOP THE FORM DATA TO SET VARS
foreach($_POST['form'] as $key=>$value){ // loop thru ur post array
$k = $key; // set a variable to the 'name' of your key - $k now = your field id/name
$$k = $value; // Yes, 2 dollar signs - this sets a variable with your fields name to its value in the post array
/*
// DEBUGGING - comment this block (down to and including "}// END DEBUG" lines
//This will ech/print everything to screen so you can check a variable has been created for all of your fields
if (!is_array($value)){
echo "$$k = ";
//echo just the value
echo "$value<br>";
}else{ // Otherwise print the key name like $myVar and print the array
echo "$".$key." = ";
print_r($value);
echo "<br>";
}// END DEBUG
*/
} // END LOOP THROUGH THE FORM KEY=>VALUES
// You might want to add die statement to prevent any emails and other actions being triggered whilst you debug. Uncomment Below
// die();
NOTE
It is not advisable that you use the same field names on your forms as the fields you are inserting them into in the database and you should escape your string data before inserting to tighten up security!
// Use the following MYSQL function around your $variables in any queries like...
'".mysql_real_escape_string($myVar)."'
Naturally, if your forms are accessible to your users, not just your admins, you will also want to do some data type/integrity checks too, e.g.
if (is_numeric($myID)){
// Then insert it
} else {
// Throw an error to warn the user that something went wrong like
die($myErrorMessage);
}
Anyway,
Hope that this is useful!
Best wishes,
Gez