cant access $form->data in form

crea2k 18 Dec, 2011
Hi, I am having major problems accessing the $form->data from within a form, if I echo $form->data['dervmargin'] in the custom code box this works fine, if I do the same from within the form, that is posted in the code tab I get nothing, also an array I create in the custom code tab cant be accessed by the form. For example if I did $equals = $value1 + value1; and then echo $equals in the custom code box it works fine. If I put echo $equals in my form nothing gets passed over even though it is loading at the same time ?.

I am trying to get this code to run :
<?php 
$cf_id = $form->data['cf_id'];

$gasoiluser = $form->data['gasoiluser'];
$gasoilmargin = $form->data['gasoilmargin'];

if ($gasoiluser=="" or $gasoiluser=="no")
$gasoilmarginfield = '0.00';
else
$gasoilmarginfield = $gasoilmargin;

$query = mysql_query("UPDATE hqfjt_chronoforms_data_addupdatelead SET gasoilmargin = '$gasoilmargin' WHERE cf_id = '$cf_id'") or die(mysql_error());
?>
crea2k 18 Dec, 2011
What I am trying to achieve is onload the form checks to see if customer uses gas oil by checking the column gasoiluser, if this value is not set or is equal to no then 0.00 is written to the profit margin column, when the form loads 0.00 should be then displayed in the profit margin box, if the customer uses gas oil, the gasoiluser column will be set to yes so the profit margin column will just be echo'd whatever it has in it.

If the customer then no longer has gas oil, the gasoiluser dropdown will be changed to no, this will then post the value no to the database, so next time the form loads the above checks will be made and the gasoiluser field will now equal no, so the profit margin field will then be cleared by having the values in it overwritten by 0.00.

This could be done with javascript but my javascript is useless so thats not an option.

my form layout for this part is basically
Is this customer use gas oil [YES/NO DROPDOWN] Profit Margin [PROFIT MARGIN]
so when the dropdown is set to NO, I obliviously dont want any data in the field profit margin as you cant have a profit margin value if the person doesnt have gas oil.
crea2k 18 Dec, 2011
I have tried another method and this doesn't work either, im really confused now ๐Ÿ˜Ÿ . If a user clicks on the name of an account in the list, it opens the edit box and loads the info into the form just fine. To test something so I can grab the values another way I tried :

$_POST['leadname'];
echo $name;

This brings up nothing.....but if I make a form field with the name 'leadname' the data is loaded just fine into the field, yet I cant echo the posted field value ?.
crea2k 19 Dec, 2011
Can anyone help me with this one ?, the next part of my project relies on being able to access the data posted to the form but I cant seem to get to it, im also not sure why I cant echo arrays defined in the custom code in the form code ?.
GreyHead 19 Dec, 2011
Hi crea2k ,

Which version of ChronoForms are you using? You can find the version from Site Admin | Extensions | Install/Uninstall | Components in Joomla! 1.5 or Site Admin | Extensions | Extension Manager | Manage in Joomla! 1.6.

Bob
crea2k 19 Dec, 2011
Hi, it is 4.0 RC2.0
GreyHead 19 Dec, 2011
Hi crea2k,

Thanks, that helps. Now, where are you using these chunks of code? I've read all your posts several times and just end up confused.

The data submitted from the form will be in the $form->data array. Any variables that you create in a Custom Code block will only be available in that code block unless you define them as global or add them to an existing global variable - like the $form->data array.

Bob
crea2k 19 Dec, 2011
I am trying to echo them in the form itself in the 'code' section into 'HTML code' , if for example I echo the $form->data['cf_id'] inside the form I get nothing, or any other field. If I then put that query into custom code it works fine and appears at the top of the page above the form.

I was trying to work out a calculation in the form by doing the following :
$gasoilprice = $form->data['gasoilmargin'] + $form->data['gasoilpurchaseprice'];
but get nothing.
If I echo $form->data['gasoilmargin'] inside a form I also get nothing.

If I do any of these calculations in the custom code they work fine but are stuck at the top of the page above the form.

The way I am loading the data into the form by the way is on my editor page I have a "for each" command, in the column next to that record is an edit button for each record, I press the edit button and the corresponding record is loading into the record edit form. Its this form that I am trying to echo info in. The form has a field called leadname , the lead name seems to load fine into that field, but if I echo it inside the form
 <?php echo $form->data['leadname'] ?> 
I get nothing at all. If I use that code in the custom code box, when the record is loaded into the editor I get the persons name appearing at the top of the page above the form.

So to round things up, I just want the code in the form 'code' box to do anything other than just just load the field info into the fields.

By the way how do you do this ? "define them as global or add them to an existing global variabl - like the $from->data array."
GreyHead 19 Dec, 2011
Hi crea2k,

How is the data loaded into your Edit form? Are you using a DB Record Loader action? If so, what is the order of the actions in the OnLoad event?

Bob
crea2k 19 Dec, 2011
I have show html, two record loaders, one that loads data from one table and stores it in the model id 'userinfo' and another that stores it in a model id 'leaddetails' , also a custom code box, this calculates values taken from the two tables and displays the results at the top of the page, and a Load JS box that loads some javascript that clears two fields when a dropdown is set to no.

That is also the order they come in from top to bottom.
crea2k 19 Dec, 2011
By the way the leaddetails loader is also set to load the fields into the forms
crea2k 19 Dec, 2011
If I use a debug on the onload I get the following :

Data Array:

Array
(
[option] => com_chronoforms
[chronoform] => addupdatelead
[token] => 8d7a16a90c1e726c8366ba79a88f9e41
[Itemid] =>
[cf_id] => 10
[cf_uid] => 8d7a16a90c1e726c8366ba79a88f9e41
[cf_created] => 2011-12-11 20:02:59
[cf_modified] => 2011-12-19 01:54:26
[cf_ipaddress] => 127.0.0.1
[cf_user_id] => 0
[viewallleads] =>
[sentletterday] => 2
[sentlettermonth] => march
[sentletteryear] => 2009
[sentpostcardday] => 4
[sentpostcardmonth] => june
[sentpostcardyear] => 2013
[sentquoteday] => NA
[sentquotemonth] => NA
[sentquoteyear] => NA
[addnewlead] =>
[leadname] => billy barker
[messagetemplates] =>
[salutation] => Mr
[emailmanagement] =>
[businessname] => Billy Barker & Son
[address] => 24 Sincil Street
[deletelead] =>
[town] =>
[address3] => boston
[address4] =>
[thankyouemail] =>
[sorryemail] =>
[reminderemail] =>
[orderthankyouemail] =>
[county] => Berkshire
[postcode] => LN5 7ET
[gasoiluser] => yes
[annualconsgasoil] => 5435
[gasoilmargin] => 2.55
[dervuser] => no
[annualconsgasderv] => 0.00
[dervmargin] => 0.00
[kerouser] => no
[annualconskero] => 0.00
[keromargin] => 0.00
[fueldetails] =>
[thankyoupostPDF] =>
[sorrypostPDF] =>
[remindpostPDF] =>
[orderthankyoupostPDF] =>
[lubesdetails] =>
[othernotes] =>
[updatelead] => Add / Update
[phone] =>
[email] =>
[createdby] => Simon Caunt
[lastvisitedday] => NA
[lastvisitedmonth] => NA
[lastvisitedyear] => NA
[oilprices] => Array
(
[0] => Array
(
[cf_id] => 1
[cf_uid] =>
[cf_created] => 2011-12-15 10:18:55
[cf_modified] => 2011-12-18 23:06:29
[cf_ipaddress] => 127.0.0.1
[cf_user_id] => 0
[oilpricegasoil] => 12.00
[oilpricederv] => 3.55
[oilpricekero] => 2.45
[button] => Submit
)

)

[userdata] => Array
(
[cf_id] => 10
[cf_uid] => 8d7a16a90c1e726c8366ba79a88f9e41
[cf_created] => 2011-12-11 20:02:59
[cf_modified] => 2011-12-19 01:54:26
[cf_ipaddress] => 127.0.0.1
[cf_user_id] => 0
[viewallleads] =>
[sentletterday] => 2
[sentlettermonth] => march
[sentletteryear] => 2009
[sentpostcardday] => 4
[sentpostcardmonth] => june
[sentpostcardyear] => 2013
[sentquoteday] => NA
[sentquotemonth] => NA
[sentquoteyear] => NA
[addnewlead] =>
[leadname] => billy barker
[messagetemplates] =>
[salutation] => Mr
[emailmanagement] =>
[businessname] => Billy Barker & Son
[address] => 24 Sincil Street
[deletelead] =>
[town] =>
[address3] => boston
[address4] =>
[thankyouemail] =>
[sorryemail] =>
[reminderemail] =>
[orderthankyouemail] =>
[county] => Berkshire
[postcode] => LN5 7ET
[gasoiluser] => yes
[annualconsgasoil] => 5435
[gasoilmargin] => 2.55
[dervuser] => no
[annualconsgasderv] => 0.00
[dervmargin] => 0.00
[kerouser] => no
[annualconskero] => 0.00
[keromargin] => 0.00
[fueldetails] =>
[thankyoupostPDF] =>
[sorrypostPDF] =>
[remindpostPDF] =>
[orderthankyoupostPDF] =>
[lubesdetails] =>
[othernotes] =>
[updatelead] => Add / Update
[phone] =>
[email] =>
[createdby] => Simon Caunt
[lastvisitedday] => NA
[lastvisitedmonth] => NA
[lastvisitedyear] => NA
)

[emailtemplateinfo] => Array
(
[0] => Array
(
[cf_id] => 12
[cf_uid] => 896445d9524ad09e74e9f520f189508a
[cf_created] => 2011-12-15 18:38:17
[cf_modified] =>
[cf_ipaddress] => 127.0.0.1
[cf_user_id] => 42
[emailformname] => Hello
[emailformmessage] => message contents bla bjkjbkj nj kjn nk nnkj nkj n
[submit] => Add / Edit
)

[1] => Array
(
[cf_id] => 9
[cf_uid] => 3be6d9e623f306901c508da6810f19de
[cf_created] => 2011-12-15 15:37:27
[cf_modified] => 2011-12-15 17:49:11
[cf_ipaddress] => 127.0.0.1
[cf_user_id] => 42
[emailformname] => Sorry
[emailformmessage] => Sorry I missed you recently, I will call again soon to arrange a meeting.
[submit] => Add / Edit
)

[2] => Array
(
[cf_id] => 11
[cf_uid] => b334ebabc04fca68e2f303f305ecddff
[cf_created] => 2011-12-15 18:31:58
[cf_modified] =>
[cf_ipaddress] => 127.0.0.1
[cf_user_id] => 42
[emailformname] => Merry Christmas
[emailformmessage] => Merry Christmas and a Happy New Year !
[submit] => Add / Edit
)

[3] => Array
(
[cf_id] => 10
[cf_uid] => b038997e709f40339abeeb35dcf80dc6
[cf_created] => 2011-12-15 17:49:31
[cf_modified] => 2011-12-19 00:33:13
[cf_ipaddress] => 127.0.0.1
[cf_user_id] => 42
[emailformname] => Order Thank You
[emailformmessage] => Thankyou for your order, we appreciate your custom, bla bla bla bla
[submit] => Add / Edit
)

)

)




Trying to echo out anything on there using echo in the 'code' tab though brings nothing, even though putting a form field in there grabs the data just fine. If you echo out any of these in the custom code box though it works fine.
GreyHead 20 Dec, 2011
Hi crea2k ,

Hmmm the data is evidently there, the order of actions looks OK. I don't see why the results aren't showing up in the form code :-(

By all means email or PM me the site URL and a SuperAdmin login and I'll take a quick look.

Bob
GreyHead 21 Dec, 2011
Hi crea2k,

I think this is working now. In the On Load event you had the Show HTML action at the top followd by the DB Record loaders. Because (most) actions are processed in the order they appear the data from the DB Record Loaders wasn't available to the Show HTML action.

Bob
crea2k 21 Dec, 2011
Brilliant !, thanks very much for your help Bob, I will try it with my local project and see if it works.
crea2k 21 Dec, 2011
Hi Bob, still no go on this one, if I go to the form code and put this at the top :
<?php
echo 'name';
echo $form->data['leaddetails']; 
?>


The output should be 'name Billy Barker' (or different name if I am viewing another contact) but all I am getting is 'name' and that's it, if you go to view all leads and click the edit button to the right of a lead it will load that lead into the form, which then should echo that field from the form data but it doesn't do anything.
crea2k 21 Dec, 2011
None of the following works either, its weird as the data must be getting to the form somehow as its displayed in the form fields.
<?php
echo 'name';
echo $form->data->leadname; 
echo $_POST["leadname"];
echo $_POST['leadname'];
?>
crea2k 21 Dec, 2011
I am just looking at the db loader and it says "The key under which the loaded record data will be stored in the form->data array.", this is called 'leaddetails' , when echoing, I dont have to echo the array AND key to do to get the data out ?.
crea2k 21 Dec, 2011
I tried echoing the key name and this seems not to work๐Ÿ˜Ÿ
echo userdata($form->data);
GreyHead 21 Dec, 2011
Hi crea2k,

Errr . . . I have no idea what that last piece of code is?

Please put a Debugger action just before the Show hTML action so that we can see what data is there.

Bob
rsearle 12 Jan, 2012
Bob,

I'm using Chronoforms_J1.6_V4_RC3.0 and I have the same problem. Here are two samples:

[list]
  • Backup of my form

  • Saved Leave Approval page, showing output from a Debugger action between the DB Record Loader and Show HTML actions
  • [/list]

    My DB loader retrieves record data, and all the fields are in $form->data when the Debugger action runs. I know this because administrator\components\com_chronoforms\form_actions\debugger\debugger.php produces output with:

    print_r2($form->data);


    All the field values are populated correctly from their namesakes on the DB record.

    Curly bracket replacement also works; the form heading is produced by:

    <h2>Approve {leavetype} Leave Application</h2>


    In addition, curly brackets replacement works regardless of the Curly brackets replacer setting in the Show HTML action.

    Yet when I write any code in any custom field, or to produce computed values for normal input or output fields, $form->data is not available in any form. Specifically, all of the following produce no output or NULL:

    [code]echo $form->data['leavetype'];
    echo $form->data['cf_id'};
    print_r($form->data);
    print_r($form);
    var_dump($form);
    any elements of $form->data used in any PHP expression anywhere[/code]

    So $form->data is available for the debugger action, for field population and for curly brackets substitution, but it is not available for custom PHP code. Can you or Max shed any light on this?

    Thanks ...
    GreyHead 12 Jan, 2012
    Hi Russell,

    I can't replicate this, maybe because I don't' have the table to load the key data.

    Where exactly are you putting what code that doesn't show you the result? I don't see any code to output $form->data in the OnLoad event of the form you uploaded here?

    Bob
    rsearle 12 Jan, 2012
    Bob,

    I've pulled the custom PHP code out of the form I sent you, because none of it worked and the form is a work in progress, I have a deadline to deliver the project next Monday. I have to keep moving on, even if I have to resort to workarounds.

    However, I've tried to insert all of these samples:

    echo $form->data['leavetype'];
    echo $form->data['cf_id'};
    print_r($form->data);
    print_r($form);
    var_dump($form);
    expressions involving $form->data['leavetype etc']


    ... in all of these places:

    [list]
  • The header text block

  • Custom elements inserted amongst the fields

  • Default expressions for existing fields, eg. name, email
  • [/list]

    ... and I get either no output where the echo or print_r should appear, or I get NULL where the var_dump should appear.

    As far as output during OnLoad is concerned, I put a Debugger action between the DB Record Loader and Show HTML actions. The saved page I sent you shows the debugger output, and also shows output from curly brackets substitution and field population, after browsing to this URL:

    http://localhost/abeyone/index.php?option=com_chronoforms&chronoform=LeaveApproval&token=0b32abd2f2bab315e4eea416791b4b40

    If it's any help, I've attached the DB table. Once you've got it loaded, the URL above (with a different site name of course) should produce the form loaded with data.

    I've also attached another version of the form, with the following expression in the Header text field:

    <p><strong>cf_id</strong> = {cf_id} = <?php echo $form->data['cf_id']; ?></p>


    If you run the form, you will see that the curly brackets expression produces the cf_id, and the PHP echo statement produces nothing. The net result is:

    cf_id = 31 =

    Thanks again ...
    GreyHead 12 Jan, 2012
    Hi Russell,

    Silly question but are you putting <?php tags around your PHP??
    <?php
    echo $form->data['leavetype'];
    ?>
    or in my preferred version
    <?php
    echo'<div>$form->data[leavetype]: '.print_r($form->data['leavetype'], true).'</div>';
    ?>


    It's tough to debug without knowing exactly what code you are using :-(

    Bob
    rsearle 12 Jan, 2012
    Yes, I did. I also added a code sample in a revised form backup I uploaded in an update to my previous message (but it might have arrived after you prepared your response to my message).

    Now I've also inserted your preferred code as a custom element on the form. The resulting output is:

    $form->data[leavetype]:



    i.e. the PHP code runs but the print_r($form->data ... statement produces no output.

    I've attached the modified form and the saved output page.

    Can you please tell me, apart from the samples I'm sending you, are you getting PHP code to output $form->data elements in custom field code on any of your own forms on Chronoforms_J1.6_V4_RC3.0?

    Thanks ...
    rsearle 12 Jan, 2012
    Bob,

    Maybe this has something to do with it. Here's recent output from the PHP interpreter in my server log:

    [12-Jan-2012 21:24:59 UTC] PHP Notice:  Undefined variable: form in C:\Zend\Apache2\htdocs\abeyone\components\com_chronoforms\libraries\includes\data_republish.php(24) : eval()'d code on line 4
    [12-Jan-2012 21:24:59 UTC] PHP Notice:  Trying to get property of non-object in C:\Zend\Apache2\htdocs\abeyone\components\com_chronoforms\libraries\includes\data_republish.php(24) : eval()'d code on line 4
    [12-Jan-2012 21:29:17 UTC] PHP Notice:  Undefined variable: form in C:\Zend\Apache2\htdocs\abeyone\components\com_chronoforms\libraries\includes\data_republish.php(24) : eval()'d code on line 4
    [12-Jan-2012 21:29:17 UTC] PHP Notice:  Trying to get property of non-object in C:\Zend\Apache2\htdocs\abeyone\components\com_chronoforms\libraries\includes\data_republish.php(24) : eval()'d code on line 4
    [12-Jan-2012 21:37:43 UTC] PHP Deprecated:  Function ereg() is deprecated in C:\Zend\Apache2\htdocs\abeyone\administrator\components\com_chronoforms\admin.chronoforms.php on line 367
    [12-Jan-2012 21:37:43 UTC] PHP Deprecated:  Function ereg() is deprecated in C:\Zend\Apache2\htdocs\abeyone\administrator\components\com_chronoforms\admin.chronoforms.php on line 370


    Are you seeing anything like this in your installation (again, for Chronoforms_J1.6_V4_RC3.0, PHP version 5.3.9 on Zend Server CE 5.6.0)?

    Here are lines 11-24 from components\com_chronoforms\libraries\includes\data_republish.php:

    class HTMLFormPostDataLoad extends JObject {
    	var $validation_errors = array();
    	
    	function load($html_code, $data = array(), $params = array()){
    		if(empty($data)){
    			//return $html_code;
    		}
    		if(!is_object($params)){
    			$params = new JParameter('');
    		}
    		$skippedarray = explode(",", $params->get('dataload_skip', ''));
    		ob_start();
    		eval( "?>".$html_code);


    The first and third log lines above (two executions of the form) imply that, at the eval statement on line 24, $html_code contains some expression involving $form, and $form does not exist at that point.

    On the basis that the problem appears to be occurring in data_republish.php, I turned off the data republishing option in the Show HTML action. Unfortunately, it made little difference: the same log errors were produced, data_republish.php is still run, and the only difference is that the non-existence of $form is now reported at line 3 of the eval'd code in $html_code instead of line 4.

    Thanks ...
    rsearle 14 Jan, 2012
    Just nudging this thread. Any clues on my last two posts, please?

    Thanks ...
    GreyHead 17 Jan, 2012
    Hi Russell,

    Sorry for the delay and I realise that this is after your deadline :-(

    I finally found time to go digging and I think I have an answer for you.

    In the Events windows Custom Code action (and other actions where PHP is allowed) the form data is in the $form->data array and can be accessed as we saw earlier.

    In the Preview window the form data is available through the curl_replacer syntax. This doesn't allow for PHP calculations but you can do these in a Custom Code action and access the results through the curl_replacer. I think this is what I've always done so far.

    I've now found that the Preview window HTML is evaluated as you saw AND when this happens the form data is in the $data array so that $data['LeaveApproval'] will evaluate to the current value.

    I modified your form to load data from the #__content articles table and I can display an srticle title using this method.

    Bob
    rsearle 20 Jan, 2012
    Bob,

    I was just getting around to cranking up a PHP debugger to find out where the retrieved data went to, but you found it! Thanks a lot!

    As it happens, my delivery went fine. I had some gaps in my forms but they were not critical to the customer and now I can go back and fix them.

    So the conclusion is that, where form data has been retrieved from an existing table record with a DB Record Loader action, the form data for elements on the Preview tab is in the $data array, not the $form->data array.

    This appears to be an anomaly, but there may be good reasons for it. I think this is important knowledge, and it should be reflected in the help text and field labels for the Custom Element and the DB Record Loader action(s), and probably the Show HTML action, at a minimum.

    As far as you know, is it also true that curly brackets replacement for elements on the Preview tab always substitutes data from the $data array rather than $form->data, or does that happen only when data has been retrieved with a DB Record Loader action?

    Thanks again ...
    GreyHead 21 Jan, 2012
    Hi Russell,

    As far as I can see the $data array is just a local copy (probably a copy by reference ) of the $form->data array. I don't see, and wouldn't expect, any connection to the source of the data. You can assume that all the form data is there until proved otherwise.

    The evaluation of the Form HTML seems to be done in the Data Republisher - whether that is enabled or not. This may be an accident of coding rather that a part of the grand plan so I guess it might change in a future release.

    Bob
    This topic is locked and no more replies can be posted.

    VPS & Email Hosting 20% discount
    hostinger