Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form in where a user inputs certain info and they get back some info via Javascript.

I want to pass the info from the Javascript on to another page. Although, in the form which the user inputs in, there is an Input field which I want to pass through to the next screen as well. I know I can pass this Input field through $POST and session, but it's the Javascript fields I'm struggling with.

Here is my form (I'll show one field as to not flood the screen with code):

//I added this hidden field and it stopped my javascript form updating//
<input id="amount" type="hidden">
//===========================================================// 
<input class="applyForm-input"  id="amount" name="amount" type="text"  autocomplete="off" maxlength="6" style="position:relative; left:200px; top:-24px;">

Here is where it is output via Javascript:

<div class="loanForm-totalToBorrow v-number" id="amountdiv">

Here is my Javascript for this particular field:

function updateCost() {

var amount = parseFloat(document.getElementById("amount").value) || 0.00;
    delivery = parseFloat(document.getElementById("delivery").value);
        if(delivery !='select' && delivery)
        {
            delivery=parseFloat(delivery);
        }
        else
        {
           delivery= 0.00;
        }
    total = amount + delivery;
    fixedrate =  total / 100 * 12.2;
    grandtotal = fixedrate + total;   

document.getElementById("amountdiv").innerHTML = amount.toFixed(2); 

Now I know PHP is serverside and Javascript is Client side. So how would I pass this data to a PHP variable.

On my second page, I want to output some of the sessions. An example of one below:

<?php
echo ' <tr class="blah1"> ';
echo ' <td class="blah2">&#163;' . $_SESSION['amountdiv'] . ' </td> ';
echo ' </tr> ';
?>

I've looked through stackoverflow and people mention hidden input fields. Although, when I add these, my Javascript stops working.

share|improve this question
    
Please check that your input does not have value.... –  Ankur Kumar Mar 15 '13 at 15:16

3 Answers 3

you can assign a php variable value to a javascript variable on page load.

$var = $_SESSION['session_var'];

<script type="text/javascript">
 var myVAr = <?php echo $var; ?>;
</script>
share|improve this answer

You posted 2 "input" fields above. The problem is, that your hidden input has the same id="amount" as you text field has.

The values of id-attributes must be unique all over your html page.

So I recommend to rename the id of the hidden input field and your javascript should work again and you may use your hidden input to post to the next page.

share|improve this answer

You can use jQuery's AJAX abilities

$(document).ready(function(){ 
    $("#mysubmitbutton").click(function(){
    var amount = parseFloat(document.getElementById("amount").value) || 0.00;
        delivery = parseFloat(document.getElementById("delivery").value);
            if(delivery !='select' && delivery)
            {
                delivery=parseFloat(delivery);
            }
            else
            {
               delivery= 0.00;
            }
        total = amount + delivery;
        fixedrate =  total / 100 * 12.2;
        grandtotal = fixedrate + total;
        .post("mysecondpage.php", {total: total, fixedrate: fixedrate, 
                                   grandtotal: grandtotal});
    });
}); 

The above grabs your JavaScript variables and posts them to the PHP page you want to redirect to

<?php

$total = $_POST["total"];
$fixedrate = $_POST["fixedrate"];
$grandtotal = $_POST["grandtotal"];

?>

You can then grab the transferred values

share|improve this answer
    
Hi Lloyd, this didn't work at all unfortunately. –  Janatan Apr 11 '13 at 13:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.