Home >Backend Development >PHP Tutorial >mysql - Destroy problem about backbone+php

mysql - Destroy problem about backbone+php

WBOY
WBOYOriginal
2016-08-04 09:19:54955browse

I implemented the deletion of data in PHP by rewriting the destroy() of backbone sync to correspond to PHP (clicking the delete button will trigger the delete event), but because the URL is defined in the collection, when rewriting the URL of destroy, Unable to perform the corresponding operation, please answer!

<code>//Collection
App.Users=Backbone.Collection.extend({
  model : App.User,
  url : function(){
    return '/crud2/save.php';
  },
  initialize : function(){
    this.on('add',function(){
      console.log(this.length);
    });
    this.on('remove',function(){
      console.log(this.length);
    });
  }
});</code>
<code>  `delete : function(){
  //this.model.url='delete.php';
  //console.log(this.model.url);
  $('#Name').val("");
  $('#Phone').val("");
  this.model.destroy({
    url : 'delete.php',
    type : 'POST',
    success : function(){
      console.log('success');
    },
    error : function(){
      console.log('error');
    }
  }
  ); 
}`

</code>
<code>//delete.php
<?php
    date_default_timezone_set('prc');
    error_reporting(E_ALL ^ E_DEPRECATED);
    header("Content-type : application/json; charset = utf-8");  
    $connection=mysql_connect("localhost","root","123456");
    if(!$connection){
        die('connect failed!');
    }else{
        mysql_select_db("backbone");
        mysql_query("set names utf-8");
        $data = json_decode(file_get_contents("php://input"));
        //echo $data;
        file_put_contents("data.txt", $data->username);
        $sql = "delete from user where username == '$data->username'";
        mysql_query($sql);
?></code>

Reply content:

I implemented the deletion of data in PHP by rewriting the destroy() of backbone sync to correspond to PHP (clicking the delete button will trigger the delete event), but because the URL is defined in the collection, when rewriting the URL of destroy, Unable to perform the corresponding operation, please answer!

<code>//Collection
App.Users=Backbone.Collection.extend({
  model : App.User,
  url : function(){
    return '/crud2/save.php';
  },
  initialize : function(){
    this.on('add',function(){
      console.log(this.length);
    });
    this.on('remove',function(){
      console.log(this.length);
    });
  }
});</code>
<code>  `delete : function(){
  //this.model.url='delete.php';
  //console.log(this.model.url);
  $('#Name').val("");
  $('#Phone').val("");
  this.model.destroy({
    url : 'delete.php',
    type : 'POST',
    success : function(){
      console.log('success');
    },
    error : function(){
      console.log('error');
    }
  }
  ); 
}`

</code>
<code>//delete.php
<?php
    date_default_timezone_set('prc');
    error_reporting(E_ALL ^ E_DEPRECATED);
    header("Content-type : application/json; charset = utf-8");  
    $connection=mysql_connect("localhost","root","123456");
    if(!$connection){
        die('connect failed!');
    }else{
        mysql_select_db("backbone");
        mysql_query("set names utf-8");
        $data = json_decode(file_get_contents("php://input"));
        //echo $data;
        file_put_contents("data.txt", $data->username);
        $sql = "delete from user where username == '$data->username'";
        mysql_query($sql);
?></code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn