Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert Backbone.js model into MySQL database
    primarykey
    data
    text
    <p>I have a backbone.js model with some defaults and an url:</p> <pre><code>var Box = Backbone.Model.extend({ url: "./save.php", defaults: { x: 0, y: 0, w: 1, h: 1 } }); </code></pre> <p>Then I have an instance of this model and I proceed to save it:</p> <pre><code>var box = new Box({ x:10, y:10, w:200, h:200 }); box.save(); </code></pre> <p>Now I want to save this model into a MySQL database using a PHP script "save.php", it goes like this:</p> <pre><code>&lt;?php include('connection.php'); $id = $_POST['cid']; $x = $_POST['x']; $y = $_POST['y']; $w = $_POST['w']; $h = $_POST['h']; mysql_query("INSERT INTO boxes (id, x, y, w, h) VALUES('$id', '$x', '$y', '$w', '$h') ") or die(mysql_error()); ?&gt; echo "Data Inserted!"; </code></pre> <p>I have tried reading many tutorials but I cannot get this simple model save to work. Why is my code not working? Any ideas on how can this be solved?</p> <p>Thanks</p> <p><strong>EDIT: QUICK SOLUTION</strong></p> <p>In the php script, the correct way to obtain the information from the sent JSON object is as follows:</p> <pre><code>$box_data = json_decode(file_get_contents('php://input')); $x = $box_data-&gt;{'x'}; $y = $box_data-&gt;{'y'}; $w = $box_data-&gt;{'w'}; $h = $box_data-&gt;{'h'}; </code></pre> <p>And to store in in the database: </p> <pre><code>mysql_query("INSERT INTO boxes(id, x, y, w, h) VALUES('', '$x', '$y', '$w', '$h') ") or die(mysql_error()); </code></pre> <p>In this way one row will be inserted in the table "boxes" with the information of each one of the attributes of the backbone model Box. The server request method in this case is POST and the id in the table "boxes" is set to auto-increment.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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