0

Cant seem to figure this out.. I have a JavaScript function that gets the value of all elements with the same class name:

var total = $(".bob").map(function() {
    return $(this).val();
}).get();

var queryString = "?total="+total;
http.open("GET", "product.php" + queryString, true);

http.onreadystatechange = rgetHttpRes;
http.send(null); 

I am passing the array to my php file -

if (isset($_GET['total'])) {
    $price = $_GET['total'];
    $num = array($price);
    $result = array_sum($num);
    echo($result);
}

// So I passed 2 integers with the JavaScript function: 15.99 and 10.99 into this  php function.
   It will only return one of them:  10.99 //

When I do this:

if (isset($_GET['total'])) {
    $price = $_GET['total'];
    $num = array($price);
    print_r($num);
}

this is the output I get:

Array ( [0] => 15.99, 10.99 ) 

I can't figure out why it wont print them like

Array ( [0] => 15.99 [1] => 10.99 ) 

Does anyone have any ideas?

2 Answers 2

4

The query is passed on as a string, you will need to use explode to convert it to an array

if(isset($_GET['total'])) {
    $price = $_GET['total'];
    $num = explode(",",$price);
    print_r($num);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i thought i had tried that.. You saved me a headache!
0

how about this one ?

var t = $(".bob").size();
var ken = new Array();
var a;
for(a = 0;a<t;a++){
ken.push($(".bob").eq(a).val());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.