0

Trying to get json array from ajax, but when i'm trying to write it down in the text file, it shows nothing.

 var img = JSON.parse(localStorage.getItem("iPath"));
                var img = JSON.stringify(img);
                console.log(img);

                $.ajax({
                    url: './php/temporary.php?deletefile',
                    cache: false,
                    type: 'POST',
                    data: img,
                    success: function( respond, textStatus, jqXHR ){

                        if( typeof respond.error === 'undefined' ){
                            //window.location.assign("/buyplace.html");
                        }
                        else{
                            console.log('ОШИБКИ ОТВЕТА сервера: ' +  respond.error );
                        }
                    },
                    error: function( jqXHR, textStatus, errorThrown ){
                        console.log('ОШИБКИ AJAX запроса: ' + textStatus );
                    }
                });

 if( isset( $_GET['deletefile'] ) ){
        $params = json_decode( $_POST);
        $myfile = fopen("testfile.txt", "w");
        fwrite($myfile, $params);
        //$img = "uploads/" . $imgPath;
        //move_uploaded_file($imgPath, "./uploads/");
        //unlink('./uploads/' . $img);
    }
    ?>

How can i solve this problem?

8
  • 1
    In your ajax call it is type of POST and the php is looking for the GET of it change the php to if( isset($_POST[''deletefile])){}. Also deletefile in your ajax call is going to be empty and not set also try changing the .php?deletefile=true Commented Feb 13, 2017 at 19:36
  • 1
    You may need to use $jsondata = json_decode(file_get_contents('php://input')) Commented Feb 13, 2017 at 19:40
  • 1
    well... $_GET['deletefile'] is on the URL line, so it should still be populated. Commented Feb 13, 2017 at 19:42
  • $_GET['deletefile'] is fine, it is the data that is not being accessed correctly. Commented Feb 13, 2017 at 19:45
  • What does img contain? Commented Feb 13, 2017 at 19:56

2 Answers 2

1

$_POST will contain key-value pairs and what you are sending, is a string.

So you should either read the standard input, or you need to make sure that you are actually sending key-value pairs.

The first case is already posted as a comment by @Scuzzy.

For the latter, using the standard key-value pairs in $_POST:

 $.ajax({
      url: './php/temporary.php?deletefile',
      cache: false,
      type: 'POST',
      data: {json: img},
      // the rest of your js

And in php:

if( isset( $_GET['deletefile'] ) ){
    $params = json_decode($_POST['json']);
    // the rest of your php
Sign up to request clarification or add additional context in comments.

2 Comments

still nothing in the file
@VitoMotorsport There is a lot that is supposed to be happening between you calling your javascript and php writing the file. You need to narrow down the problem.
0

There's no need to send the parameters as JSON. You can use an object as the data: option, and each property will be sent as the corresponding $_POST element.

var img = JSON.parse(localStorage.getItem("iPath"));
console.log(img);

$.ajax({
    url: './php/temporary.php?deletefile',
    cache: false,
    type: 'POST',
    data: img,
    success: function( respond, textStatus, jqXHR ){
        if( typeof respond.error === 'undefined' ){
            //window.location.assign("/buyplace.html");
        }
        else{
            console.log('ОШИБКИ ОТВЕТА сервера: ' +  respond.error );
        }
    },
    error: function( jqXHR, textStatus, errorThrown ){
        console.log('ОШИБКИ AJAX запроса: ' + textStatus );
    }
});

In PHP, you'll need to use json_encode() to convert the $_POST array to a string that can be written to a file.

if( isset( $_GET['deletefile'] ) ){
    $params = $_POST;
    $myfile = fopen("testfile.txt", "w");
    fwrite($myfile, json_encode($params));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.