Forums

Show submitted values using On Submit php code?

sinemac 22 Mar, 2009
How can I pass the values submitted for some of the form fields to the post-submission page?

I thought it might work to simply leave the redirect URL blank, and then use some php code in the 'On Submit code - after sending email' area, but it isn't working.

I tried this:
Thanks, you will receive $<?php echo $_POST['card']; ?> in the form of a Gift Card, and<br />
$<?php echo $_POST['cash']; ?> in cash in early July, and<br />
$<?php echo $_POST['shares']; ?> in shares.


But it results in this:

Thanks, you will receive $ in the form of a Gift Card, and
$ in cash in early July, and
$ in shares.



Can this be done? If so, what am I missing?

Thanks,
Scott
GreyHead 22 Mar, 2009
Hi Scott,

I'd expect that to work OK.

Add
echo '$_POST: '.print_r($_POST, true).'<br /><br />';
to output the $_POST array and check what is there.

Bob
sinemac 22 Mar, 2009
Thanks, Bob!

That helped me track down the problem - I feel a bit stupid on this one...

The fields I was trying to show up on the next page are calculated values I have automatically updating in the form based on earlier fields. In order to make sure they could show up on the form, but not be changed by the user, I set them as 'disabled', so of course, they weren't showing up on the post-submit page. 😶

So, what I've got right now is this in the form:
<div class="clearfix"></div>
<div class="formelement clearfix">
	<div class="formlabel2">Amount of your claim as filed in the Proposal:</div>
	<div class="inputelement clearfix"> $<input type="text" name="claim" onChange="updatethis(this.form);" /></div>
</div>
<div class="formelement clearfix">
	<div class="formlabel2">Amount of your claim that you would like paid through the <em>Gift Card</em>:</div>
	<div class="inputelement clearfix"> $<input type="text" name="giftcard" onChange="updatethis(this.form);" /></div>
</div>

and this:
<p>Based on your claim and requested <em>Gift Card</em> value entered above, you will receive<br />
$<input disabled type="text" length="6" name="card" /> in a <em>Gift Card</em>, and<br />
$<input disabled type="text" name="cash" length="6" /> in cash, and<br />$<input disabled type="text" name="shares" /> in shares.</p>


And this javascript:
function updatethis(form) {
  form.elements['cash'].value = Math.min(500, form.elements['claim'].value - form.elements['giftcard'].value);
  form.elements['shares'].value = form.elements['claim'].value - form.elements['giftcard'].value - form.elements['cash'].value;
form.elements['card'].value = form.elements['giftcard'].value;
}


So, I guess what I really need to know is if there's a better way to show those calculated values in the second form snippet so that they aren't editable, but so that they also still get passed with rest of the data on submit? (In the email also).

Or should I create additional form elements for the same values in the javascript, and then put them in the form as 'hidden'?
sinemac 22 Mar, 2009
Did some testing - creating additional form elements in the javascript for the same values, and then adding them to the form as 'hidden' elements does work.

I'm open to suggestions if there's a 'better' way - but this does work, so I'm happy. 8)

But I do have a new issue/question - I've discovered that my calculation is a bit off... e.g., 2000.57 - 500 - 1400.25 = 100.31999999999994

Ignoring for the moment that it's wrong - is there something I can add to the javascript to force it to round to 2 decimal points?

Thanks,
Scott
GreyHead 22 Mar, 2009
Hi Scott,

If you use readonly='readonly' instead of disabled then the values should be carried through OK.

Or it's sometimes cleaner to add the values into hidden fields and simple change the innerHTML of spans to display the results.

Bob

PS Just playing with a different version here . . .
sinemac 22 Mar, 2009
Thanks again Bob!

Using readonly appears to work just fine - and keeps things from getting too bloated with unnecessary code.


-Scott
GreyHead 22 Mar, 2009
Hi Scott,

Early morning here (or it was). I created this version that rounds the numbers, checks the giftcard isn't more than the claim and puts the answers into spans. All this code is in the FormHTML box:
<?php
$doc =& JFactory::getDocument();
$script = "
window.addEvent('domready', function() {
  var source = $$('input.source');
  source.addEvent('change', function() {
    var card = 0;
    var cash = 0;
    var shares = 0;
    var claim = $('claim').value;
    claim = (claim * 100).round()/100;
    $('claim').value = claim;
    card = $('giftcard').value;
    if ( card == '' ) { 
      card = 0.00; 
    } else {
      card = (card * 100).round()/100;
      card = Math.min(card, claim);
      $('giftcard').value = card;
    }
    cash = Math.min(500, claim - card);
    cash = cash.toFloat().round(2);
    shares = (claim - card - cash);
    shares = (shares*100).round()/100
    $('card').value = card;
    $('cash').value = cash;
    $('showcard').setHTML(card);
    $('showcash').setHTML(cash);
    $('showshares').setHTML(shares); 
  });
});
";
$doc->addSCriptDeclaration($script);
?>
<div class="clearfix"></div>
  <div class="formelement clearfix">
    <div class="formlabel2">Amount of your claim as filed in the Proposal:</div>
    <div class="inputelement clearfix"> $<input class='source' type="text" name="claim" id='claim' /></div>
  </div>
  <div class="formelement clearfix">
    <div class="formlabel2">Amount of your claim that you would like paid through the <em>Gift Card</em>:</div>
    <div class="inputelement clearfix"> $<input class='source' type="text" name="giftcard" id='giftcard' /></div>
</div>

<p>Based on your claim and requested <em>Gift Card</em> value entered above,<br /> you will receive $<span id='showcard'>0</span> in a <em>Gift Card</em>; $<span id='showcash'>0</span> in cash, and $<span id='showshares'>0</span> in shares.</p>
<input type="hidden" name="card" id='card' value='' />
<input type="hidden" name="cash" id='cash' value='' />
<input type="hidden" name="shares" id='shares' value='' />
<input type='submit' name='submit' value='Confirm' />

Bob
sinemac 22 Mar, 2009
Cool - thanks Bob!

Not only does that reduce the amount of code compared to what I ended up with, I'm learning some good stuff in the process!

-Scott
sinemac 22 Mar, 2009
Wouldn't you know it... now I've got a new question.

How would I adjust the code to do three things:

[list]
  • make it so that '.00' is added automatically if the user doesn't include it

  • deal with commas (e.g., 2,500.00) if someone puts one in (currently it changes to NaN if a comma gets in there)

  • make sure there are two decimal places even if rounding would lead to one (e.g., 2000.52 - 100.02 = 1900.5 which is how it currently ends up -- although I suppose #1 would deal with this also)
  • [/list]

    Thanks,
    Scott
    sinemac 22 Mar, 2009
    I managed to figure out part of it... I got the forced two decimal places for the calculated values by adding '.toFixed(2)' in a couple of places:

    <?php
    $doc =& JFactory::getDocument();
    $script = "
    window.addEvent('domready', function() {
      var source = $$('input.source');
      source.addEvent('change', function() {
        var card = 0;
        var cash = 0;
        var shares = 0;
        var claim = $('claim').value;
        claim = (claim * 100).round()/100;
        $('claim').value = claim;
        card = $('giftcard').value;
        if ( card == '' ) { 
          card = 0.00; 
        } else {
          card = (card * 100).round()/100;
          card = Math.min(card, claim);
          $('giftcard').value = card;
        }
        card = card.toFixed(2);
        cash = Math.min(500, claim - card);
        cash = cash.toFloat().round(2).toFixed(2);
        shares = (claim - card - cash);
        shares = (shares*100).round(2)/100;
        shares = shares.toFixed(2);
        $('card').value = card;
        $('cash').value = cash;
        $('showcard').setHTML(card);
        $('showcash').setHTML(cash);
        $('showshares').setHTML(shares); 
      });
    });
    ";
    $doc->addSCriptDeclaration($script);
    ?>
    <div class="clearfix"></div>
      <div class="formelement clearfix">
        <div class="formlabel2">Amount of your claim as filed in the Proposal:</div>
        <div class="inputelement clearfix"> $<input class='source' type="text" name="claim" id='claim' /></div>
      </div>
      <div class="formelement clearfix">
        <div class="formlabel2">Amount of your claim that you would like paid through the <em>Gift Card</em>:</div>
        <div class="inputelement clearfix"> $<input class='source' type="text" name="giftcard" id='giftcard' /></div>
    </div>
    
    <p>Based on your claim and requested <em>Gift Card</em> value entered above,<br /> you will receive $<span id='showcard'>0</span> in a <em>Gift Card</em>; $<span id='showcash'>0</span> in cash, and $<span id='showshares'>0</span> in shares.</p>
    <input type="hidden" name="card" id='card' value='' />
    <input type="hidden" name="cash" id='cash' value='' />
    <input type="hidden" name="shares" id='shares' value='' />
    <input type='submit' name='submit' value='Confirm' />


    That may actually be enough - I probably don't need to have it adjust the user's input to add '.00'

    I haven't looked for a solution for the comma yet - but I could just indicate in the form that only numbers and decimal points are allowed (the easy way out! :wink: )

    Scott
    GreyHead 22 Mar, 2009
    Hi Scott,

    You can use the standard ChronoForms validation to force the number input.

    Bob
    sinemac 22 Mar, 2009
    Ah, good point... don't know why I didn't think of that. I was going down a path that would have made it more complicated than it needs to be!

    Thanks again, Bob! You've been a great help!

    Scott
    sinemac 24 Mar, 2009
    Hi, it's me again! 🤣

    I've been trying to figure out how to have the system automatically 'force' (or add) two decimal points to the input fields (claim and giftcard) if the user doesn't do so, without breaking the existing code. So far, I haven't had any luck.

    I did manage to get the two decimal places added to the calculated values, even if they were '.00' - but I can't seem to get it to add '.00' to the user input values (which I think would need to be done with an onChange statement).

    Currently, the two input fields end up with no decimal places if the value is x.00 -- even if I put in the .00, they are removed when I move to the next field.

    I've tried a few different things on my own, but haven't managed to get it to work.

    Any suggestions?

    Thanks,
    Scott
    sinemac 24 Mar, 2009
    I just tried something else, and I did get it to keep the '.00' when input by the user - so that's a good step, and may be enough (although if it would be easy to add a snippet that would cause the system to add it if left out, that would be better).

    What I did that seems to be working to keep the .00 when input is changed this:
     var claim = $('claim').value;


    to this:
     var claim = $('claim').value.toFixed(2);


    Seems that was enough so that the .00 doesn't get removed - but it doesn't add it if it's left out.

    Scott
    sinemac 24 Mar, 2009
    Turns out I was wrong... adding '.toFixed(2)' did break the calculation part.

    I've also discovered that if someone inputs anything with the two decimal places ending in '0', then the last 0 is removed when you move onto the next field.

    So, for example, 1300.50 changes to 1300.5 when you move to the next field in the form (and 1300.00 becomes 1300).

    I haven't had any luck figuring out how to 'fix' that without breaking the rest of it... so I'm open to suggestions.

    Thanks,
    Scott
    Max_admin 25 Mar, 2009
    Hi Scott,

    did you try google or mootools forums ?🙂

    Max
    Max, ChronoForms developer
    ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
    ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
    sinemac 25 Mar, 2009
    Hi Max,

    I did try Google, and tried a few of the potential solutions I found - but each time I got the two decimal places working on the input fields, I managed to break the calculation.

    I haven't tried the Mootools forums... but I'll give that a try.

    Thanks for the suggestion.

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