0

I am serializing a string to be able to send to a php script, but the function leaves the string with / instead of /. I would like to remove the \ from the string.

Can you help me ?

The function I use :

$scope.serialize = function(obj, prefix) {
    var str = [];
    for(var p in obj) {
        if (obj.hasOwnProperty(p)) {
          var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
          str.push(typeof v == "object" ?
            serialize(v, k) :
            encodeURIComponent(k) + "=" + encodeURIComponent(v));
        }
    }
    return str.join("&");
}

I am using this function to send to a file :

$scope.send = function(){

    // sendToServer.f($scope.serialize($scope.line))
    sendToServer.f($scope.line)
        .success(function(){
            console.log('SUCESS ! sent to angualar-seed/db.jsonp, using appendToDb.php !')
        })
        .error(function(){
            console.log('ERROR !')
        });
}

FYI the function to send the string to php is :

services.js:

.service('sendToServer', [
    '$http',
    function ($http) {
        'use strict';

        this.f = function (dataToSend) {
            return $http({
                url: 'http://id:pwd@bodlip.com/angular-seed/appendToDb.php',
                method: "POST",
                data: dataToSend,
                headers : {
                    'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
                }
            });
        };
    }
])

I thought I should do it in php, and I put this in my php file :

 $data[] = $_POST;

//file_put_contents('log.txt', print_r($data, true), FILE_APPEND);

function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}

$data = stripslashes_deep($data);
Louis
  • 2,548
  • 10
  • 63
  • 120
  • That... looks like a poorly chosen approach in the first place. Why not just send the data as JSON (e.g. using `JSON.stringify()` on the JS side) and parse it using `json_decode()` in PHP? – Ilmari Karonen Sep 16 '16 at 12:58
  • oh ok, could you please help rewrite the code for json ? – Louis Sep 17 '16 at 19:18
  • I'm not really familiar enough with Angular to code up an example for you off the top of my head, but Googling for something like "angular php post json" turns up e.g. [this tutorial](https://codeforgeek.com/2014/07/angular-post-request-php/) and [this highly voted question on SO](http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined). – Ilmari Karonen Sep 17 '16 at 19:32
  • thanks a lot ! I am gonna check those links out. – Louis Sep 17 '16 at 19:40
  • Actually it doesn't work. The string I send is allready in JSON format. – Louis Sep 19 '16 at 10:36

1 Answers1

0

I added this line to my code:

$line = str_replace('\\', '', $line);
Louis
  • 2,548
  • 10
  • 63
  • 120