Bohdi
Bohdi

Reputation: 1305

Passing variables from PHP to Javascript back to PHP using Ajax

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:

$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.

I am using MS SQL DB.

Upvotes: 1

Views: 412

Answers (3)

Yes Barry
Yes Barry

Reputation: 9846

Check your function signature.

You have:

function ajaxCases(squares,start,price){

Should be:

//                 |-------- capital S
function ajaxCases(Squares,start,price) {

Or better yet, change this:

$('#step1price').load("ajax-step1-price.php?Squares="+Squares);

To This:

//                                                    v--- lowercase s
$('#step1price').load("ajax-step1-price.php?Squares="+squares);

JavaScript variable are case-sensitive.

Upvotes: 1

Minras
Minras

Reputation: 4346

Try debugging it.

First look into resulting HTML generated by PHP.

If all values are correct, try running the javascript generated for <input name="squares" onclick="..." /> in browser console.

If the call from ajaxCases() goes to the server, look into the URL - whether it is correctly formed.

If it is - check what the server script returns and what it does with input values.

Hope that helps.

Upvotes: 0

John Dvorak
John Dvorak

Reputation: 27277

the Squares is indeed a GET variable, you can access it like this: $_GET['Squares']:

If $_GET['Squares'] returns undefined, verify that you calling the correct URL.

Upvotes: 0

Related Questions