Modify $_POST array before plugins or email

dauk 19 Jan, 2009
I must be overlooking something really stupid, but I cannot seem to modify the $_POST array.

I have a form that (among other things) asks for the user's zipcode. In the code section "On Submit code - before sending email" I query a zipcode table which gives me the users $city and $state. At this point I assumed that I would be able to just assign the $city and $state values to two hidden fields in the form.
like:
$_POST['hidden_0'] = $city;
$_POST['hidden_1'] = $state;


or maybe:
JRequest::setVar('hidden_0',$city,'POST','true');


When the email is sent the hidden_0 and hidden_1 fields are empty and the registration plugin does not receive the values either. I tried running the same basic code in the Validation tab and got the same results. I must have some false assumptions regarding the order of execution of something. A push in the right direction is appreciated!

Thanx!
-Dauk
GreyHead 19 Jan, 2009
Hi dauk,

As far as I can see that should work OK. Do you have Send Emails set to On in the General Tab? (If not then the OnSubmit before box doesn't run.)

Bob
dauk 19 Jan, 2009
Yeah, I've got email activated on the General tab. I've attached a backup of the a test form if you wanted to see the configuration. I receive emails when this form is submitted but the contents of the test field are not modified by the on_submit block of PHP.

<scratching head>
-dauk


form html:
<div  class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label">Click Me to Edit</label>
<input class="cf_inputbox" maxlength="150" size="30" id="text_0" name="text_0" type="text">
</div><div class="clear"> </div></div>



On submit code:
<?php
JRequest::setVar('text_0','this is new text','POST');
?>
GreyHead 19 Jan, 2009
Hi dauk,

This works OK for me. I edited your test form to add two output lines to the OnSubmit code
<?php
echo "_POST: ".print_r($_POST, true)."<br /><br />";
$array = $_POST;
$array['text_0'] = 'this is new text';
//JRequest::setVar($array, 'POST', true );
JRequest::setVar('text_0','this is new text','POST');
echo "_POST: ".print_r($_POST, true)."<br /><br />";
?>
and when I submit the form I see this

_POST: Array ( [text_0] => wserwer [68fd3a3fb70d82bba95c57adc5635920] => 1 )

_POST: Array ( [text_0] => this is new text [68fd3a3fb70d82bba95c57adc5635920] => 1 )

so the value is changing OK.

I think that your problem is that the email template fields are evaluated before the OnSubmit Before code has run. (Thst isn't necessarily helpful but it's how ChronoForms is set up.)

We've looked at this before and a workaround is to put a proxy (like ##zip##) in place in the email template and to use str_replace substitute it in the OnSubmit Before box. There's an example in this thread

Bob
dauk 19 Jan, 2009
Thanks GreyHead! I looked all over before posting, yet somehow I still missed that thread. 😀
-Dauk
dauk 19 Jan, 2009
Hmm, this workaround mostly works but doesn't get the revised $_POST values into the cf_cb_registration plugin only the email. Do you happen to have a workaround to get the values into the cf_cb_registration plugin? I'll look more closely at the $plugins array and see if I can update something there.

Thanks for the help
-Dauk
dauk 20 Jan, 2009
It seems that there are some quirks when the cf_cb_registration plugin is activated by normal methods (plugin checkbox). I have not really documented any of the details that I observed, but I would be happy to if it would lead to bugfixes in these routines. There seemed to be timing issues with sending the CF email, creating an account using cf_cb_registration (not happening), and using validation to check the fields. So, I put everything I needed into validation instead. In validation I dropped some of the plugin initialization stuff from chronocontact.php, check that username/email have not been used yet, do a zipcode lookup to get consistent city/state info, and create the user account. Kinda an ugly hack at the moment but I'll be cleaning that up.

I should say that I *really* do appreciate how flexible ChronoForms is! I mean, what other module allows you to 'roll-your-own-$whatever' like ChronoForms 😀
The term "enough rope to hang yourself with" comes to mind too.. but I prefer the positive spin

-Dauk!
PS: Here's the code^H^H^H^Hhack for interested parties
<?php
global $mainframe;
// Currently in: On Submit Code - before sending email
$username = $_POST['text_8']; //JRequest::getVar('text_8','','post'); //
$email    = $_POST['text_1']; //JRequest::getVar('text_1','','post') ;//
$zipcode  = $_POST['text_2']; //JRequest::getVar('text_2','','post') ;//
   
$DB =& JFactory::getDBO();
//verify that the username and email address are available
$query = "SELECT `username`,`email` FROM #__users WHERE `username` = '$username' OR `email` = '$email'";
$DB->setQuery($query);
$rows  = $DB->loadObjectList();
$error = 0;
$error_ms = '';
foreach ($rows as $row) 
    {
    if ($email == $row->email) 
        {
        $error = 1;
        $error_ms .= "An account already exists with the email: $email ($row->email) <br />";
        }
    if ($username == $row->username) 
        {
        $error = 1;
        $error_ms .= "An account already exists with the username: $username ($row->username) <br /> ";
        }
    }
$error_ms .= 'Please choose another or if this is your email just use the <a href="index.php/Lost-password.html">\'lost password\'</a> utility<br />';
if ($error > 0) {$error = 0; return $error_ms;}

$query = "SELECT * from zip_code WHERE `zip_code` = '$zipcode'";
$DB->setQuery($query);
$rows  = $DB->loadObjectList();
$city  = $rows[0]->city;
$state = $rows[0]->state_prefix;

// if the query failed then the zipcode must not be in our database
if ($city == '' || ! isset($city)) 
    {
    $error = "Please re-enter zipcode or entere a nearby zipcode.<br />The zip '$zipcode' does not exist in our database";
    return $error;
    }
$_POST['hidden_0'] = $city;  //JRequest::setVar('hidden_0',$city,'post');//
$_POST['hidden_1'] = $state; //JRequest::setVar('hidden_1','post');//


//<START>We'll call the plugin ourselves MANUALLY w00t!
$formname = JRequest::getVar( 'chronoformname');
$query = "SELECT * FROM #__chrono_contact WHERE name='$formname'";
$database->setQuery( $query );
$rows = $database->loadObjectList();
$ava_plugin = 'cf_cb_registration';
if ($debug) {$mainframe->enqueueMessage("before query: ID: ".$rows[0]->id);}
$query = "SELECT * FROM #__chrono_contact_plugins WHERE form_id='".$rows[0]->id."' AND event='ONSUBMIT' AND name='".$ava_plugin."'";
$DB->setQuery( $query );
$plugins = $DB->loadObjectList();

if ($debug) {$mainframe->enqueueMessage('before plugins test count:'.count($plugins)." plugins");}
if(count($plugins)){
	require_once(JPATH_SITE."/components/com_chronocontact/plugins/cf_cb_registration.php");
    if ($debug) {$mainframe->enqueueMessage('loaded cf_cb_registration');}

	${$ava_plugin} = new $ava_plugin();
	$registry3 = new JRegistry();
	$registry3->loadINI( $plugins[0]->params );
	$params = $registry3->toObject( );  
    if ($debug) {$mainframe->enqueueMessage($ava_plugin .'->calling onsubmit');}
    ${$ava_plugin}->onsubmit( 'com_chronocontact', $params, $plugins[0] );
}
//<END>End of calling plugin code

print "
<p>Thanks for registering $_POST[text_7]!  A confirmation email has been sent to $_POST[text_1].  Please read the email and follow the activation link to complete your registration.</p>
<p>*If you do not find the email in your INBOX then please make sure to check your spam filter/junk mail.</p>";
?>

Max_admin 21 Jan, 2009

Hmm, this workaround mostly works but doesn't get the revised $_POST values into the cf_cb_registration plugin only the email. Do you happen to have a workaround to get the values into the cf_cb_registration plugin? I'll look more closely at the $plugins array and see if I can update something there.

Thanks for the help
-Dauk



Hi dauk,

did you pay attention that there is some way to change the order or execution of the "onSubmit" code and the pugins..etc ? you have to know the correct order the code will use to run!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
dauk 21 Jan, 2009
Yes, I saw that but even after trying the various combination's it still had some quirky behavior. I'm sure there probably was a workable method without the hack I posted earlier; I'm just starting to run out of time on this specific project.

-Dauk
Max_admin 21 Jan, 2009
No problems, there is also an order button "before/after email" in the plugin config page itself, combining this with the other order tab in the form edit page and this will offer too many options!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.