2

I am relatively new to cake and I am struggling with a custom filter that im making in order to display products based on which checkboxes have been ticked. The checkboxes get populated based on which attributes the user creates in the backend, i then collect all the values of the selected boxes into an array with javascript and post it to the controller, but for some reason I cannot access the controller variable named '$find_tags' in my view, it throughs undefined variable. Here is my javascript and ajax which collects and posts correctly (when i firebug it 'data' in my controller has array values which im posting) so thats fine

$("#clickme").click(function(event){    
    event.preventDefault();
    var searchIDs = $("#checkboxes input:checkbox:checked").map(function(){
        return $(this).val();
    }).get();

    var contentType = "application/x-www-form-urlencoded";
    var data = 'data[ID]='+searchIDs;
    $.post("",data,function(data){
        console.log(data);
    });

    });

Here is my controller code which im assuming is where the fault lies

if ($this->request->is('post') ) {
     $data = $this->request->data['ID'];
     $find_tags = array();   
     $selected_tags = $data;
     foreach($selected_tags as $tag)
     {
        array_push($find_tags,$this->Product->findByTag($tag));
        $this->set('find_tags', _($find_tags));
     }
  }

And here is my view code where i get Undefined variable: find_tags

foreach($find_tags as $all_tag)
  {
  echo $all_tag['Product']['name'];
  echo '</br>';
  }

Any help or suggestions would really be appreciated been struggling with this for a while now

2
  • It's most likely because $data is empty. Why don't you echo data and see what is returned? I'm willing to bet the post is not working right. in CakePHP you should be forming your URL like this -> http://mysite.com/controller/function/argument1/argument2. Commented Aug 23, 2013 at 21:13
  • why do you have no URL/URI specified in jQuery post() function parameters? Commented Aug 23, 2013 at 21:43

1 Answer 1

1

If searchIDs is array of ids you just need to make the json of array and then send to your controller

$("#clickme").click(function(event){    
    event.preventDefault();
    var searchIDs = $("#checkboxes input:checkbox:checked").map(function(){
        return $(this).val();
    }).get();

    var contentType = "application/x-www-form-urlencoded";
    var data = 'ids='+JSON.stringify(searchIDs);
    $.post("controller url",data,function(data){
        console.log(data);
    });

    });

On php side you are getting wrong variable

if ($this->request->is('post') ) {
     $data = $this->request->data['ids'];
     $find_tags = array();   
     $selected_tags = $data;
     foreach($selected_tags as $tag)
     {
        array_push($find_tags,$this->Product->findByTag($tag));

     }
   $this->set('find_tags', _($find_tags));
  }
Sign up to request clarification or add additional context in comments.

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.