1

I passed an array using Ajax and i echoed it in php , it works good. But i don't know how to separate its value and find its length.

Here is the code:

var myCheckboxes = new Array();
        $(".book_type:checked").each(function() {
           myCheckboxes.push($(this).val());
        });

            $.post("s.php?c="+category+"&k="+myCheckboxes,{data : "some data"}, function(response){
   $("#show_result").html(response);

And the php is:

$a=$_GET['k'];
echo $a;

it displays the values of all checkbox like this

all,0,1,2,3,4

How can i find $a length as array?. if i use sizeof($a) it shows as 1. Also how to separate those values into each single value. Any suggestion

4 Answers 4

1

try like this

$a=$_GET['k'];
$value= explode(",",$a);
echo sizeof($value); //output 6
echo $value[0];      //all
echo $value[1];      //0
echo $value[2];      //1
echo $value[3];      //2
echo $value[4];      //3
echo $value[5];      //4
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah..it works...thank you. will accept your answer.
1

Try using:

$a=$_GET['k'];
$a=explode(',', $a);
echo count($a);

Comments

0

Pass the value as an array

$.post("s.php", {
    data: "some data",
    c: category,
    k: myCheckboxes
}, function (response) {
    $("#show_result").html(response);
})

then

$a=$_GET['k'];

where $a will be an array, so count($a) should give you the length

Comments

0

You should call JSON.stringify in your javascript:

//                   ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓
... category+"&k=" + JSON.stringify(myCheckboxes) ...

or, alternatively, use jQuery ajax syntax sugar in your post request:

$.post("s.php", { k: myCheckboxes, ... });

This will provide a json as string passed to controller. From within your controller you should json_decode the received parameters:

$a = $_GET['k'];
print_r(json_decode($a));

Hope this helps.

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.