Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form refresh and submit

    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:

    Code:
    <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:
    <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:
    <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;"/>

    Questions:
    1. 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?
    2. 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.

    Thanks,
    Robert

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    One solution would be to handle the tax calculation when a state is selected entirely on the Javascript side. I guess it depends on what PHP is doing with the selected state. If it's a fairly simple matter, such as getting a tax rate out of a table, you can simple pre-load a Javascript array using PHP.

    I'm probably not making much sense... it would look something like this:
    PHP Code:
    <script type="text/Javascript">
    var taxRates = new Array(
    <?php
    $sql 
    'select taxrates from blah blah blah';
    $result mysql_query($sql);
    if (!
    $result) {
        die (
    'error ' mysql_error());
    }
    for (
    $i 0$i mysql_num_rows($result); $i++) {
        
    $taxVal mysql_fetch_assoc($result);
        
    //need a comma between array values on all but the first element
        
    if ($i != 0) {
            print 
    ",\n";
        }
        
    //create the Javascript array values here
        //i.e. print "('Arizona', 'AZ', 6.75)
        
    print "('{$taxVal['stateDesc']}', '{$taxVal['stateCode']}', {$taxVal['stateTaxRate']})\n";
    }
    ?>
    )
    </script>
    This should create an Javascript array that you can then use entirely on the client side when the drop down is used, which allows you to avoid going back and forth from server to client just to handle the drop down behavior.

    If you're not keen on that idea, there are other ways of doing it...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •