Buy Now
Sign in

Simple php echo in form html

chuanse , April 24 2018
C
chuanse 68
April 24 2018 #377239
Hello people;
pulling my hair out on the simplest thing... I made multiple forms with Chronoforms, yet I can't seem to get this right:
on Preview tab I have a custom code field:
 <p><?php echo $dbstatus; ?></p>
for debug even added
<p><?php echo $dbcount; ?></p>
and
<p><?php echo $dbfree; ?></p>
and in the OnLoad I have a query (tested with phpmyadmin! -> result returns 1)
<?
$db123 = &JFactory::getDBO();
$query123 = "
SELECT COUNT(*) FROM `#__chronoforms_data_movienight020518`;
";

$db123->setQuery($query123);
$dbcount = $db123->loadResult();

$dbfree = 200-$dbcount;

if ($dbfree >50 ) {
$dbstatus = 'some text';
}
elseif ($dbfree >0 AND <50 ) {
$dbstatus = 'some other text';
}
else {
$dbstatus = 'alternative text';
}
?>
Yet on the form page none of the three values want to show!
What am I doing wrong?? :-(
Thanks for any feedback
C
C
chuanse 68
April 24 2018 #377240
ps: It is CF 4.0.1!
GreyHead 63.3
April 24 2018 #377246
Hi chuanse,
You need to save the values to the $form->data array so that they are available to other parts of the form e.g.
$form->data['dbstatus'] = 'some text';
and
echo "<p>{$form->data['dbstatus']}</p>";
Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
C
chuanse 68
April 24 2018 #377252
Hi Bob
wel I did try that...
Could it be that is not possible to show this data array on the OnLoad page?
Custom Element:
<p>{form->data['dbcount']}</p>
<p>{form->data['dbfree']}</p>
<p>{form->data['dbstatus']}</p>
And in OnLoad custom code, mode 'controller'
<?
$form->data['dbstatus'] = 'dbstatus';
$form->data['dbcount'] = 'dbcount';
$form->data['dbfree'] = 'dbfree';
?>
Stays blank :/
C
chuanse 68
April 24 2018 #377253
ok, one step further,
if i do not use curly brackets, but use php instead.... it shows text. condition:
onload custom code, mode controller
$form->data['dbstatus'] = 'dbstatus'; 
$form->data['dbcount'] = 'dbcount';
$form->data['dbfree'] = dbfree;
custom element
<p>php-dbcount: <?php echo $form->data['dbcount']; ?></p>
<p>php-dbfree: <?php echo $form->data['dbfree']; ?></p>
<p>php-dbstatus: <?php echo $form->data['dbstatus']; ?></p>
If I use the db query and load the result into the form data aray, it shows up blank. :/
custom element
<p>php-dbcount: <?php echo $form->data['dbcount']; ?></p>
<p>php-dbfree: <?php echo $form->data['dbfree']; ?></p>
<p>php-dbstatus: <?php echo $form->data['dbstatus']; ?></p>
custom code onload, mode controller
<?
$db123 = &JFactory::getDBO();
$query123 = "
SELECT COUNT(*) FROM `#__chronoforms_data_movienight020518`;
";

$db123->setQuery($query123);
$dbcount = $db123->loadResult();

$dbfree = 200-$dbcount;

if ($dbfree >50 ) {
$dbstatus = 'GELUKKIG, JE BENT ER OP TIJD BIJ!';
}
elseif ($dbfree >0 AND <50 ) {
$dbstatus = 'HAAST JE! ER ZIJN SLECHTS X PLAATSEN MEER VRIJ!';
}
else {
$dbstatus = 'TE LAAT! ALLE PLAATSEN ZIJN HELAAS GEBOEKT...';
}

$form->data['dbstatus'] = $dbstatus;
$form->data['dbcount'] = $dbcount;
$form->data['dbfree'] = $dbfree;
?>
sigh, I know the query works... :/
GreyHead 63.3
April 25 2018 #377266
Hi chuanse,
I'm sorry I don't understand what the problem is exactly.
What is 'mode controller'?
What do you see if you add a Debugger action at the end of the On Load event? Please copy and paste the Debugger output here.
Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
C
chuanse 68
April 25 2018 #377274
Hello Bob
I figured it out... I made the same mistake before.
For the FORM WIZARD EVENTS TAB:
In custom code (in onload or onsubmit) php "if" statement is not supported fully. "elseif" is not supported, nor are multiple "if" statements
=> custom code has 2 choices (controller => early processing OR view)
You can do:
if (some phprule) { $something = 'text'; } else { $something = 'text'; }
But you can't do:
if (some phprule) { $something; } elseif { $something; } else { $something; }
Nor can you do multiple if statements :
if (some phprule) { $something }
if (some phprule) { $something }
if (some phprule) { $something }
You must indeed place the php output in the array as you stated before
form->data['something'] = $something;
Then for the preview tab => add a custom element and use brackets only!
<p>{something}</p>
In my case this code works:
Events tab, OnLoad action, custom code (mode controller):
<?php
$user = &JFactory::getUser();
$form->data['netid'] = $user->username;

$db123 = &JFactory::getDBO();
$query123 = "
SELECT COUNT(*) FROM `#__chronoforms_data_movienight020518`;
";

$db123->setQuery($query123);
$dbcount = $db123->loadResult();

$dbfree = (200-$dbcount);

if ($dbfree >=1 ) {
$dbstatus = 'WE HAVE FREE SEATS';
}
else {
$dbstatus = 'TOO LATE, FULLY BOOKED';
}
$form->data['dbstatus'] = $dbstatus;
?>
Preview tab, custom element:
<p>{dbstatus}</p>
I hope this helps others, I am using Chronoforms 4.0.1 (yes i am behind :-/ )