0

I have a onclick and post function in jQuery where I pass two variables to my PHP function.

The functions look like following:

$(".pop-save-us").click(function(){
    console.log(checkbox_events);
    console.log(user_id);
  // alert("Button save pressed");
    $.post('/user/AddRemoveBrandUsers', { brands: JSON.stringify(checkbox_events),userId:user_id} , function(data) {
        if(checkbox_events.length==0)
        {
            alert("No changes were made.")
        }
        else {
            if (data == "ok") {
             alert("ok");
            }
        }
    });

});

The first value is an array in following format:

console.log(checkbox_events) gives a following output:

[2: "checked", 4: "checked", 5: "checked"]

I do `JSON.stringify(checkbox_events) to turn my array into JSON format and pass it to my PHP function like following:

 public function AddRemoveBrandUsersAction()
    {
        $brands = $this->getRequest()->getPost("brands");
        $userId = $this->getRequest()->getPost("userId");
        for($i=0;$i<count($brands);$i++)
        {
         // how can I now access each value of the brands array 
         // I need both key and value... How do I access them ??
        }
        die("ok");
    }
5
  • 1
    So, you wanna use a foreach($array as $key => $value) ? Commented Jun 8, 2016 at 10:35
  • Okay can you reply in a form of answer so that I can accept ur answer.... And how do I get the "checked" value inside the loop ?? Commented Jun 8, 2016 at 10:36
  • What do you mean by "checked" value? If you are referring to a checkbox or radio, then the value / input is only sent if they are checked? Commented Jun 8, 2016 at 10:38
  • You dont check if its checked on PHP side, you only pass the checked value to server side. Commented Jun 8, 2016 at 10:38
  • yes thats what i ment danny... u can see that I have the "checked" stored in the array and then passed to PHP function. Question is, how can I access the: [2: "checked" << this value $key => $value is going to be =2 , but how to access the "checked" as well ?? Commented Jun 8, 2016 at 10:42

2 Answers 2

1

use the code follows:

 if(!empty($brands) && sizeof($brands) > 0){
     foreach($brands as $key => $value){
         ...
     }
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Okay the $key is going to be the = 2 value... But how do i get the string value which is "checked" inside the loop? :)
$value is 'checked' text
and $key is =2 ? :)
exactly $key is 2 and $value is 'checked'
0

You should use

json_decode(%your_string_with_json%) 

function to get the internal php representation of your json string. Thus, you will be able to manipulate it (get data, set data). It's very useful to use debugger also.

$a = json_decode('{"foo": 123, "bar": null, "baz": "qwerty", "arr": ["hello", "world"]}', true);

Will give you and array, which you can easily use within your code:

array (
  'foo' => 123,
  'bar' => NULL,
  'baz' => 'qwerty',
  'arr' => 
  array (
    0 => 'hello',
    1 => 'world',
  ),
)

For example:

$b = $a['foo'] // b = 123
$b = $a['arr'][0] // b = hello

Then, you should watch the value you have received from the request, and depending on it, use if ... else ... and etc.

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.