Forums

is it possible to have program computation in the forms?how?

strk 04 Feb, 2009
Hello, I need to do coding too, but haven't figured out yet.
The closest I get to something usable so far has been putting the actual PHP code into the EMAIL template (horrible, isn't it?).

What I need to do is take form input and compute the something to be sent by email and if possible also stored in the db.

My initial thought has been to create an additional form input for the result (to be put in an HIDDEN-type input) and then
modify it using real computation on submit (before email).

Questions:

1) Is this possible ?
2) Can you show an example of *reading* form data (I found the mosGetParam call in one of the tabs and it worked, but not sure it's guaranteed to always work)
3) Can you show an example of *writing* form data ? using mosSetParam in email template resulted in an internal server error...

Thanks in advance.
GreyHead 04 Feb, 2009
Hi strk,

Which version of Joomla are you using (the code is quite different between 1.0 and 1.5)?
and what calculation do you want to do?

Bob
strk 05 Feb, 2009

Hi strk,

Which version of Joomla are you using (the code is quite different between 1.0 and 1.5)?
and what calculation do you want to do?

Bob



Joomla is 1.0.15.
I'll need to compute the total cost of a reservation.

If possible, the result of this computation should go in the database too, but it is probably
also fine to just send it in the email that is sent to both user and site manager.

Chronoengine is version 2.3.9.
GreyHead 05 Feb, 2009
Hi strk,

The *usual* way to do this is to add JavaScript to the form so that the result shows in the browser before the form is submitted.

Here's how to do it in PHP after the form is submitted.
[list=1]
  • Add a hidden input to your form with a name='total'
  • In the OnSubmit code do something like
    <?php $quantity = $_POST['quantity'];
    $price = $_POST['price'];
    $_POST['total'] = $quantity * $price;
    ?>
  • [/list:o]You should then be able to use the 'total' field in your emails and save it in the database.

    Bob
    strk 05 Feb, 2009

    Here's how to do it in PHP after the form is submitted.
    [list=1]

  • Add a hidden input to your form with a name='total'
  • In the OnSubmit code do something like
    <?php $quantity = $_POST['quantity'];
    $price = $_POST['price'];
    $_POST['total'] = $quantity * $price;
    ?>
  • [/list:o]You should then be able to use the 'total' field in your emails and save it in the database.



    Didn't work. I have the hidden field, with a default value of 0.
    I recompute the value like you say, but still get 0 as a result from {expansion}.
    I also tried avoiding any computation and just setting $_POST['costo'] = 'x', but still get 0 back.
    Yes, I have the 'costo' hidden field.
    GreyHead 05 Feb, 2009
    Hi strk,

    Leave the default value of the hidden fields empty value='' (though is doesn't make much difference).

    Turn on DeBug in the General tab, submit the form and post the print out of the $_POST array here please.

    Bob
    strk 05 Feb, 2009
    _POST: Array ( [name] => kl [email] => xxxx [citta] => [telefono] => xxx [data_arrivo] => [settimane] => 10 [textarea] => [bambini_3] => 0 [bambini_12] => 0 [bambini_26] => 0 [adulti] => 0 [adulti_60] => 0 [igloo] => 0 [materassini] => 0 [tenda3] => 0 [tenda4] => 0 [Messaggio] => [chrono_verification] => xi8Qv [Submit] => INVIA [costo] => 0 )
    Case 2: Use template
    name: kl email: xxxxt citta: telefono: xxx data_arrivo: settimane: 10 textarea: bambini_3: 0 bambini_12: 0 bambini_26: 0 adulti: 0 adulti_60: 0 igloo: 0 materassini: 0 tenda3: 0 tenda4: 0 Messaggio: chrono_verification: xi8Qv Submit: INVIA costo: test E-mail: 'Yes' custom
    Email sent

    ------------- So far it seems the onSubmit executed ('cost' became 'test' from the original '0'), but keep reading:

    E-mail message

    From: xxx
    To: xxx
    Subject: xxxx

    Costo totale: 0

    ------------ Here, the 'Costo totale' value (0) is expanded from {costo}
    GreyHead 05 Feb, 2009
    Hi strk,

    Do you have the calculation code in the OnSubmit Before box?

    And Send Emails set to On in the General Tab?

    Bob
    strk 05 Feb, 2009

    Do you have the calculation code in the OnSubmit Before box?

    And Send Emails set to On in the General Tab?



    Yes to both.
    On Submit code - before sending email: $_POST['costo'] = "test";
    GreyHead 05 Feb, 2009
    Hi strk,

    [sendfb][/sendfb]
    Bob
    strk 05 Feb, 2009
    Attached is the form backup, from a local installation of joomla 1.0.15
    and ChronoForms V2.3.9 J1.0.
    GreyHead 05 Feb, 2009
    Hi strk,

    Thanks - it's late here, I'll take a look first thing in the morning.

    Bob
    GreyHead 06 Feb, 2009
    Hi strk,

    My memory is going :-(

    The OnSubmit Before is executed **after** the field values are inserted in the html message body - weird but true!

    We've worked around this before by adding in a place holder to the email and substituting this in the onSubmit before code.

    If you set the value of the hidden field to say '##@@##' you can do a string replace in the OnSubmit code to substitute the calculated value.

    Bob
    strk 06 Feb, 2009

    The OnSubmit Before is executed **after** the field values are inserted in the html message body - weird but true!



    Gah, pretty ugly. What about fixing the bug instead ?
    strk 06 Feb, 2009
    Patch for chronocontacts.php (just moving the pre e-mail code execution where it belongs):

    251,256d250
    < /**
    < * Run the On-submit 'pre e-mail' code if there is any
    < */
    < if ( !empty($rows[0]->onsubmitcodeb4) ) {
    < eval( "?>".$rows[0]->onsubmitcodeb4 );
    < }
    402a397,402
    > /**
    > * Run the On-submit 'pre e-mail' code if there is any
    > */
    > if ( !empty($rows[0]->onsubmitcodeb4) ) {
    > eval( "?>".$rows[0]->onsubmitcodeb4 );
    > }
    GreyHead 06 Feb, 2009
    Hi strk,

    Feature - bug who knows :-)

    Max is the coder and has control over the code & distribution. I don't change anything as there is a risk of the download copies here being out of sync with the master code and the licensed distros.

    Bob
    Max_admin 06 Feb, 2009
    Hi,

    at V3.0 for J1.5 the "onsubmit before" is executed BEFORE we generate the email🙂 unfortunately I quit any coding for V2.3.9 long time ago, they will stop supporting it at the Joomla official site very soon too! its time to upgrade!

    Regards
    Max
    Max
    ChronoForms developer...
    Did you try ChronoMyAdmin for managing your Joomla database tables ?
    pauwiks 01 Jun, 2009
    Hi,

    I've read the post and can you please show how to do this with joomla 1.5?

    I'm a newbie with coding. What I want done is:

    DROP DOWN BOX
    choose game to play: lotto 6/49 (the value should be 22.50)
    lotto 6/45 (the value should be 12.50)
    lotto 6/42 (the value should be 12.50)

    TEXT FIELD -
    can i set the field
    to xx-xx-xx-xx-xx-xx ?
    1. 6 Digit combination: xx-xx-xx-xx-xx-xx (the value should be the same as what the player chose in the drop down box)
    2. 6 Digit combination: xx-xx-xx-xx-xx-xx (the value should be the same as above and it should be added)

    Submit --> After submitting what is the best option to show the result?
    Can it be seen in the confirmation page plug-in?

    Thanks. Hope you can help me here.
    Max_admin 01 Jun, 2009
    Hi pauwiks,

    No need to use the Confirmation page plugin here!

    I'm also not sure I could understand the problem so please some real numbers!

    Cheers
    Max
    Max
    ChronoForms developer...
    Did you try ChronoMyAdmin for managing your Joomla database tables ?
    GreyHead 01 Jun, 2009
    Hi Pauwiks,

    All of this is possible and is fairly straight-forward. The questions you are asking are better answered from a good HTML tutorial; you'll find many on the web - those at W3Schools are pretty reliable. Or you may be better off getting a more knowledgeable friend to help you with the basic form coding.

    Bob
    pauwiks 01 Jun, 2009
    Hi,

    I went to w3schools and studied the HTML Forms, but the tutorials does not include what I want to be done. Also I don't have any programmer friends. I'm the only one in my group that knows how to do basic html.😀

    I attached a .jpg file showing what I want to happen. How you can point me to the right direction. Thanks!

    Pauwiks
    Max_admin 05 Jun, 2009
    Hi,

    Please show me your form HTML code!

    Regards
    Max
    Max
    ChronoForms developer...
    Did you try ChronoMyAdmin for managing your Joomla database tables ?
    This topic is locked and no more replies can be posted.