0

Hello,

there is a function which saves and sends post request:

save_car: function(e) {

      if (e.keyCode != 13) return;
      if (!this.brand.val()) return;

      Cars.create({brand: this.brand.val(), color: this.color.val(), max_speed: this.max_speed.val() });

      this.brand.val('');
      this.color.val('');
      this.max_speed.val('');
    },





Request URL:http://localhost/backbone/car_list/backend/index.php/welcome/index
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,lt;q=0.8,en-US;q=0.6,ru;q=0.4,pl;q=0.2
Connection:keep-alive
Content-Length:70
Content-Type:application/json
Cookie:PHPSESSID=5dndo82fup9pi838f9uterrci6; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%227a7e53ab078672515aec9b4b8a73cb83%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%29+AppleWebKit%2F53%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1368858542%3B%7D48f6946a4ed6a07726f1d2790a67993c
Host:localhost
Origin:http://localhost
Referer:http://localhost/backbone/car_list/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
X-Requested-With:XMLHttpRequest
Request Payloadview source
{brand:sad, color:sf, max_speed:sd, order:1, remove:false}
brand: "sad"
color: "sf"
max_speed: "sd"
order: 1
remove: false

So as we see from headers there is post request with various parameters.

Now the php function:

public function index()
    {

        $this->load->model('Backbone_model');

        //$res = $this->Backbone_model->get_all();

        if (isset($_GET)) {
            echo json_encode($this->Backbone_model->get_all());
        }
        else if (isset($_POST)) {

            echo 'ts';

            //SELECT name as brand, address as color, tel as max_speed FROM contacts

            $sql = "INSERT INTO contacts (name, address, tel) VALUES (?, ?, ?)";
            $this->db->query($sql, array($_POST['brand'], $_POST['color'], $_POST['max_speed'] ));

            echo $this->db->last_query();

        }

        echo 'asss';

        print_r($_REQUEST);

    }

it returns;

[{"brand":"Darius","color":"maciuleviciaus 30","max_speed":"866612345"},{"brand":"arvydas","color":"zidiku","max_speed":"866"}]asssArray()

so it looks like it see $_GET request. WHy this could be? It returns json and later string 'asss'

2
Contributors
7
Replies
17
Views
4 Years
Discussion Span
Last Post by McLaren
0

changed checking a bit:

else if ($_SERVER['REQUEST_METHOD'] == 'POST') {

and now it recognizes post. But still dont see any variables:

print_r($_POST);

just shows: Array()

0

Without seeing the snippet of code that performs the POST "we" are unable to suggest where the issue lies.

0

Without seeing the snippet of code that performs the POST "we" are unable to suggest where the issue lies.

save_car: function(e) {
      if (e.keyCode != 13) return;
      if (!this.brand.val()) return;
      Cars.create({brand: this.brand.val(), color: this.color.val(), max_speed: this.max_speed.val() });
      this.brand.val('');
      this.color.val('');
      this.max_speed.val('');
    },

There is Cars.create

The create method in backbone triggers they sync.

http://backbonejs.org/#Sync

The default sync handler maps CRUD to REST like so:

create → POST /collection
read → GET /collection[/id]
update → PUT /collection/id
delete → DELETE /collection/id

0

I've never used backbone, and just trying to get my head around it's concepts.. but the documentation appears to suggest that the .create will fire a "request"

Creating a model will cause an immediate "add" event to be triggered on the collection, a "request" event as the new model is sent to the server, as well as a....

I may be way off :-)

0

btw I have heard that globals are bad practice.

So I want to ask is using $GLOBALS['HTTP_RAW_POST_DATA'] bad? Then how can I replace it? WHen there is no data in $_POST array.

But I googled a bit and there is different thing - global $var; so syntax looks bit different, so maybe only this is bad and $GLOBALS is not bad?

0

Found how to get $_POST working:

in javascript document readu function add:

Backbone.emulateJSON = true;

and now we can see there is index 'model' in $_POST array. Its json encoded so just decode and use.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.