Passing variables from PHP to Javascript back to PHP using Ajax.

Posted by ObjectiveJ on Stack Overflow See other posts from Stack Overflow or by ObjectiveJ
Published on 2012-10-15T09:29:13Z Indexed on 2012/10/15 9:37 UTC
Read the original article Hit count: 203

Filed under:
|
|
|

I hope this makes sesne, please bare with me. So I have a PHP page that contains variables, I have some radial boxes, and on click of them, it calculates a price for the item you have clicked on. I do this by activating a js function that I have passed some variables to. Like so.

PHP:

<?php 

    $result = mssql_query("SELECT * FROM Segments ORDER BY 'Squares'");

        if (!$result) {
        echo 'query failed';
        exit;

            }

        while ($row = mssql_fetch_array($result)) { ?>



 <span><?php echo $row["Squares"]; ?></span><input name="squares" type="radio" onclick="ajaxCases('<?php echo $row["Squares"]; ?>', '<?php echo $row["StartCaseID"]; ?>', '<?php echo $row["StartMatrixPrice"]; ?>')" value="<?php echo $row["Squares"]; ?>"<?php if ($row["Squares"] == "1") { ?> checked="checked" <?php }else{ ?> checked="" <?php } ?>/>
                            <?php } ?>

As you can see onclick it goes to a function called ajaxcases, this function looks like this.

function ajaxCases(squares,start,price){
    $('#step1').html('<p style="margin:100px 0px 0px 100px"><img src="images/ajax-loader-bigindic.gif" width="32" height="32" alt="" /></p>');
    $('#step1').load("ajax-styles.php?squares="+squares);
    prevId1 = "";
    document.varsForm.caseid.value=start;
    $('#step1price').html('<span style="margin:0px 0px 0px 30px"><img src="images/ajax-loader-price.gif" width="24" height="24" alt="" /></span>');
    $('#step1price').load("ajax-step1-price.php?Squares="+Squares);
    return true;
}

This then goes to a php page called ajax-step1-price.php and I try to recall the variable Squares. However it doesn't work, I thought it was a GET however that returns undefined.

In Summary: I would like to know how to pass a variable from PHP to JS then back to PHP, or if someone could just tell me where I am going wrong that would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript