Welcome to WebmasterWorld Guest from 3.227.233.78

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP form & calculations

     
11:49 am on Mar 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Oct 3, 2007
posts:8
votes: 0


Hi all,

I'm creating a form for a website and I need to have a calculation performed on one of the form inputs (a numerical value).

Basically, the user will enter a value into the field (x) in the form, and submit it as usual. This information will then all be processed and emailed to the site owner, and take the user to a second page which will say "Please wait... Calculating your value" or something similar. It'll then go to a third page which will display a value based on a calculation (x * 0.82) and ask the user to confirm the offer.

To give you a better idea of the process, <snip> is pretty much the sort of thing we're looking at.

Is anyone able to help or point me in the right direction?

[edited by: dreamcatcher at 12:44 pm (utc) on Mar. 10, 2008]
[edit reason] no urls as per T.O.S [webmasterworld.com].Thanks [/edit]

12:21 pm on Mar 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Feb 6, 2008
posts:33
votes: 0


This is fairly easy but I don't think the middle page with the 'Calculating your value' is really necessary.

Say if your input box has the 'name' attribute of 'input', get the form to submit to say confirm.php with the method of 'POST'.

Eg:

<form action="confirm.php" method="post">
<input name="input" type="text" />
</form>

The code on confirm.php could contain something like

<?PHP

//Your calculation
$value = $_POST['input'] * 0.82;

//Output result
echo $value;

?>

This is obviously very simplified but should do what you want it to.

1:18 pm on Mar 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Oct 3, 2007
posts:8
votes: 0


Thanks for your reply. My PHP knowledge really is quite limited - could you expand on your example please?

Also, the calculating page is used to give the impression that it's searching through a database of similar properties and doing a lot of calculations - it's all faff, but what the client wants... :-)

3:47 pm on Mar 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Feb 6, 2008
posts:33
votes: 0


In that case 3 files:


form.php -

<form action="confirm.php" method="post">
<input name="input" type="text" />
</form>


confirm.php -

<body onload="document.valform.submit();">
<form name="valform" action="complete.php" method="post">
<input name="input" type="hidden" value="<? echo $_POST['value']; ?>"/>
</form>

Please wait... Calculating your value

</body>


complete.php

<?PHP

//Your calculation
$value = $_POST['input'] * 0.82;

//Output result
echo $value;

?>

For the second file look for a javascript function to submit after a certain number of seconds otherwise it will just submit as soon as it loads but usually displays the text for a split second.

3:52 pm on Mar 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Oct 3, 2007
posts:8
votes: 0


Thanks! Will try that now!
4:03 pm on Mar 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Oct 3, 2007
posts:8
votes: 0


It's not carrying the value over to the final page - it's giving a 0 result every time. When posting directly from form.php to complete.php, however, it does work. It seems that confirm.php isn't carrying the input somehow.

Do you have any ideas?

4:52 pm on Mar 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Feb 6, 2008
posts:33
votes: 0


Hmmmm... try taking the onload script out of the body tag and then when it takes you to the next page view the source to see if the value is coming through properly.
8:15 pm on Mar 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Oct 3, 2007
posts:8
votes: 0


Nope - this is what comes through:-

<input name="input" type="hidden" value=""/>

9:58 am on Mar 11, 2008 (gmt 0)

New User

10+ Year Member

joined:Feb 6, 2008
posts:33
votes: 0


Sorry. that's me being an idiot.

Change the code on confirm.php to:
<body onload="document.valform.submit();">
<form name="valform" action="complete.php" method="post">
<input name="input" type="hidden" value="<? echo $_POST['input']; ?>" />
</form>

1:06 pm on Mar 11, 2008 (gmt 0)

New User

10+ Year Member

joined:Oct 3, 2007
posts:8
votes: 0


Marvellous, thanks!

Am I also able to carry data from other fields through? These ones will remain hidden behind the scenes and are only carried through so that they can be submitted to the website owner once the value has been calculated.

4:54 pm on Mar 11, 2008 (gmt 0)

New User

10+ Year Member

joined:Feb 6, 2008
posts:33
votes: 0


Absolutely.

Just set the field type to 'Hidden'.

Say if your input box is like this:

<input name="input1" type="hidden" />

To display this in the php file you've posted the form to just type:

<?php echo $_POST['input1']; ?>

You can then obviously assign this to a variable or whatever you wnat to do with it.