Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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:

$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

share|improve this question
1  
Your code would be a lot cleaner if you moved your embedded styles into a CSS file and used a class or id attribute instead. That is best practice for HTML/CSS anyway.. – mmmshuddup Oct 15 '12 at 9:44

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
unless he's deliberately referring to a global variable that does not respect the naming convention. :-) – Jan Dvorak Oct 15 '12 at 9:49
@JanDvorak :) yes that is true – mmmshuddup Oct 15 '12 at 9:50

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.

share|improve this answer

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.

share|improve this answer

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.