Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save JSON data in cakephp
    text
    copied!<p>I'm using CakePHP v2.2.1 stable. I have a <code>UsersController</code> with the action <code>add()</code>. I'm trying to send the user info via ajax (from the home page to <code>/users/add</code>) and save the data. My code is something like this:</p> <pre><code>// /app/View/Pages/home.ctp &lt;?php $data = array('User' =&gt; array('username' =&gt; 'vegeta_super_sayajin', 'password' =&gt; 'over9000!', 'email' =&gt; 'vegeta@supersayajin.com', 'profile_pic' =&gt; '/home/pics/scouter.jpg', 'firstname' =&gt; 'Vegeta', 'lastname' =&gt; 'Vegeta', 'level_id' =&gt; '9001')); ?&gt; &lt;script type="text/javascript"&gt; var data = &lt;?php echo json_encode($data); ?&gt; //convert $data into json format $.ajax({url: '/users/add', data: "data="+data, type: 'post'}); &lt;/script&gt; </code></pre> <p>How do I receive this data in the <code>UsersController</code>, so that I can process and save it?</p> <p>Currently, I'm trying:</p> <pre><code>// /app/Controller/UsersController.php function add() { if($this-&gt;request-&gt;is('post') { //returns "Error: [object Object] in logfile $this-&gt;log($this-&gt;request-&gt;data); } else { $this-&gt;Session-&gt;setFlash(__("The user could not be saved :(")); } $this-&gt;autoRender = false; } </code></pre> <p><code>$this-&gt;log($this-&gt;request-&gt;data)</code> returns <code>Error: [object Object]</code> in the <code>/app/tmp/logs/error.log</code> file, and this user info does not exist in any of <code>$this-&gt;request-&gt;params</code>'s indexes. All my googling so far has returned only complicated cakephp v1.3 techniques. How is this done in cakephp v2.2.1?</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