Page 1 of 1

Form refresh and submit

Posted: Mon Feb 26, 2007 8:33 am
by Rostok
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,
I have a PHP page with submission form for collectiong billing information from the customer. When the customer selects his state of residence from a dropdown list, the page is refreshed and some tax calculation as performed. I’m using the “onChange” event for dropdown list to call a JavaScript, which then checks which state is selected, updates a value for a hidden field in the form and reloads the page. The value of this hidden field is then used by php code in the form to calculate the tax.
Here is JavaScript code:

[syntax="html"]<script type='text/javascript'>
function calculateTax()
{
	state = document.forms['checkoutform'].elements['billstate'];
	var stateVal = state.options[state.selectedIndex].value;
	if(stateVal == 'VT')
	{
		document.forms['checkoutform'].elements['tax'].value = 0.07;
	}
	document.forms['checkoutform'].submit();
}
</script>
Everything so far is working great, but I have a problem. In order for the page to reloaded (to update the changed value of the hidden field), the
form has to post to itself, as I understand it.

Code: Select all

<form name="checkoutform" action="" method="post" onsubmit="return validateCard(this.cardnumber.value,this.cardType.value,this.cardmonth.value,this.cardyear.value);">
But after the form is filled with customer information, when the submit button is pressed, all information in the form should be validated (I have credit card validation script and required fields validation script included) and then the form should be posted to other php page, for inserting the customer information into database and sending the confirmation e-mail.

Code: Select all

<form name="checkoutform" action="confirmorder.php" method="post" onsubmit="return validateCard(this.cardnumber.value,this.cardType.value,this.cardmonth.value,this.cardyear.value);">
.
.
.
<input type="submit" value="Make Purchase" onClick="MM_validateForm('namefull','','R','email','','RisEmail','phonework','','R','billaddress1','','R','billcity','','R','billzip','','R');return document.MM_returnValue;"/>
My question is: How can I refresh the page/form and make some calculation when the dropdown list changes his value and at the same time to be able to submit the form to other php page?

Thanks,
Robert


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Feb 26, 2007 8:38 am
by Begby
First off I would make any thing that has anything to do with tax calculations run server side, its too easy for people to change. Someone could easily edit and change it to a negative percentage value, then they would actually be getting a discount. Maybe show them what the tax will be, but recompute it server side for sure. Also, never rely on javascript for form validation, always double check it server side.

Anyways, to answer your question, you can change the value of a hidden field without posting a form, all you have to do is tell javascript to change the value of the element like document.getElementById('myhiddenfield').value = 0.7. (you are going to want to double check the syntax).

Posted: Mon Feb 26, 2007 9:03 am
by Rostok
Thanks for your quick response, Begby.

You're right, the tax calculation should be performed server-side (as it is now; the actual calculation is run with php code). I think that the setting of the sales tax percentage (hidden field value) could be also done by server-side, but that shouldn't be a problem to do.
My real concern is how to return the changed value of the hidden field from JavaScript, so the php code knows that some state is selected from the dropdown list and that it should run the tax calculation accordingly? And if this can be done without refreshing the page (or posting the form), my problem is solved.

The entire page code isn't mine; I was asked just to add the omitted sales tax calculation and I should do that without significant change in the page structure, if it's possible of course :)

Also, I not very experienced with JavaScript nor PHP, so any actual code example will be of great help.

Thanks,
Robert

Posted: Mon Feb 26, 2007 9:53 am
by feyd
PHP can read the selected item from the drop-down without added help from Javascript unless the <select> isn't in the form; which would be silly.