Note that there are some explanatory texts on larger screens.

plurals
  1. POcakephp, jquery, .ajax()
    text
    copied!<p>I've been stuck with the retrieving of my sent variable trough an ajax POST function. Could you help me out?</p> <p>My Jquery code:</p> <pre><code>$.ajaxSetup ({ cache: false }); var selected = new Array(); $(document).ready(function() { $('.value').click(function () { if($(this).hasClass('strong')) { selected.splice(selected.indexOf(this.innerHTML), 1); submitData(selected); $(this).removeClass('strong'); } else { selected.push(this.innerHTML); submitData(selected); $(this).addClass('strong') } }); }); function submitData(arDat) { var arrayData = {"param1" : JSON.stringify(arDat)}; $.ajax({ type: 'POST', url: 'http://localhost.local/coconut/trunk/challenges/values', data: arrayData, dataType: 'json', success: function(data){ console.log(arrayData); }, error: function(message){ alert(message); } }); } </code></pre> <p>My CakePHP Controller function:</p> <pre><code>function values() { if ($this-&gt;RequestHandler-&gt;isAjax()) { $this-&gt;autoRender = false; Configure::write('debug', 0); $params = json_decode($_POST['param1']); //$result = json_encode($params); $this-&gt;set('submitValue', $params); } else { $this-&gt;redirect(array('controller' =&gt; 'challenges', 'action' =&gt; 'index')); } } </code></pre> <p>And in a view.ctp file:</p> <pre><code>&lt;?php debug($submitValue); ?&gt; </code></pre> <p>But I get the following error: Notice (8): Undefined variable: submitValue</p> <p>In firebug I see this:</p> <pre><code>Parametersapplication/x-www-form-urlencoded param1 ["Business","Life","Health"] Source param1=%5B%22Business%22%2C%22Life%22%2C%22Health%22%5D </code></pre> <p>Does anyone know what I'm doing wrong?</p> <p>Thanks!</p> <p>Edit:</p> <p>A bit more clarification about what I want.. I want to use $this->set('submitValue', $params); (so $submitValue) elsewhere in another view.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload