How to use backbone.js with php to save data into mysql database

backbone.js, mysql, php

Solution

When you call the .save() method a JSON object is sent to the url you have specified; if the model is new, the POST method will be used. When the JSON is received in the PHP script you have to decode it and save the model attributes, let us say one field per attribute. Change this in the 'inserts.php':

<?php 
    include('conn.php');

    $data = json_decode(file_get_contents('php://input'));
    $name = $data->{'name'};
    $password = md5($data->{'password'});
    $email = $data->{'email'};

    //proceed to add every variable to a field in the database
    //...

Problem

I'm just getting started with backbone.js and I'm working with this tutorial on Nettuts. http://net.tutsplus.com/tutorials/javascript-ajax/getting-started-with-backbone-js/ ``` var user = Backbone.Model.extend({ initialize: function(){ console.log('user was initialized!'); }, defaults:{ 'name' : 'wern', 'full_name' : 'rem falkner', 'password' : 'secret', 'email' : 'secret@gmail.com' } }) var u = new user() ``` And then I used the save method: ``` u.save(undefined, {url : 'inserts.php'}) ``` Inserts.php contains: ``` <?php include('conn.php'); $name = $_POST['name']; $password = md5($_POST['password']); $email = $_POST['email']; $db->query("INSERT INTO tbl_users SET user_id='$name', pword_hash='$password', full_name='$name', email='$email'"); ?> ``` What's wrong with my code? It seems to be inserting in the database because whenever I call the save() method it inserts something on the user table but only in the password field.

Original source