1

i really struggle to get the POST value in the controller .i am really new to this..Please someone share me some light..being in the dark for long hours now. i had a checkboxes and need to pass all the ids that had been checked to the controller and use that ids to update my database.i don't know what did i did wrong, tried everything and some examples too like here: sending data via ajax in Cakephp

found some question about same problem too , but not much helping me( or maybe too dumb to understand) . i keep getting array();

please help me..with my codes or any link i can refer to .here my codes: my view script :

<script type="text/javascript"> $(document).ready(function(){ $('.checkall:button').toggle(function(){ $('input:checkbox').attr('checked','checked'); $('#button').click( function (event) { var memoData = []; $.each($("input[name='memo']:checked"), function(){ memoData.push($(this).val()); }); var value = memoData.join(", ") //alert("value are: " + value); //start $.ajax({ type:"POST", traditional:true; data:{value_to_send:data_to_send}, url:"../My/deleteAll/", success : function(data) { alert(value);// will alert "ok" }, error : function() { alert("false submission fail"); } }); //end } ); //end of button click },function(){//uncheck $('input:checkbox').removeAttr('checked'); }); }); 

my controller :

public function deleteAll(){ if( $this->request->is('POST') ) { // echo $_POST['value_to_send']; //echo $value = $this->request->data('value_to_send'); //or debug($this->request->data);exit; } } 

and result of this debug is:

\app\Controller\MyController.php (line 73) array() 

Please help me.Thank you so much

3
  • You write in ajax data:{value_to_send:data_to_send}, , Are you putting data_to_send the value? Where var value = memoData.join(", "); Commented May 27, 2016 at 4:44
  • oh sorry about that, it supposed to be a data:{value_to_send:value}. i was playing around with the codes before. but still the problem is there. everytime i debug, its look like there's nothing have been pass to the controller...:( Commented May 27, 2016 at 6:06
  • Have you tried this answer Commented May 27, 2016 at 10:46

2 Answers 2

1

How about this:

Jquery:

$(document).ready(function() { $('.checkall:button').toggle(function() { $('input:checkbox').attr('checked','checked'); $('#button').click(function(event) { var memoData = []; $.each($("input[name='memo']:checked"), function(){ memoData.push($(this).val()); }); //start $.ajax({ type: 'POST', url: '../My/deleteAll/', data: {value_to_send: memoData}, success : function(data) { alert(data);// will alert "ok" }, error : function() { alert("false submission fail"); } });//end ajax }); //end of button click },function(){//uncheck $('input:checkbox').removeAttr('checked'); }); }); 

In controller:

public function deleteAll() { $this->autoRender = false; if($this->request->is('Ajax')) { //<!-- Ajax Detection $elements = explode(",", $_POST['value_to_send']); foreach($elements as $element) { //find and delete } } } 
Sign up to request clarification or add additional context in comments.

4 Comments

hello sir, Thank you so mch for helping me.i try your solution and with the couple of modification on the codes, i manage to get those data passed to my controller. But i have some question to ask , how can i do the find and delete on the foreach($elements as $element)? . i am sorry, i really lost when its come to this.Thanks again
The best way would be to pass ids ($elements = ['1','3','6',...]) and then only you would have to search by id. $post = $this->Posts->get($element) If it´s not, then just find element by some thing e.g: 'name' $post = $this->Posts->find()->where(['name' => $element])->first() Now you can delete element. $this->Posts->delete($post)
Thank you so much sir for helping me to understand this. after fixing and modified the code as needed, i manage to get it work :)
@winwizardy I`m glad to help you ;)
0

You need to set the data type as json in ajax call

JQUERY CODE:

$.ajax({ url: "../My/deleteAll/", type: "POST", dataType:'json', data:{value_to_send:data_to_send}, success: function(data){ } }); 

1 Comment

Thank you so much for this, i tried this, but somehow its still gave me an error." false submission fail". when i debug with chrome i catch this error : message : "Unexpected token i in JSON at position 0" . so tried the solution above and modified a bit and it goes ok without problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.