0

I am sending a string and an array to a php file from an AJAX request

My html looks like this

<form>
  <input class="description" type="text">
  <input type="submit">
</form>

My js file looks like this

$('form').submit(function(e){

    e.preventDefault();

    var description = $('.description').val();

    var fileNames = ['1.jpg', '2.jpg'];

    var data = {
        description,
        fileNames
    };

    $.ajax({
        url: 'details.php',
        type: 'POST',
        data,
        success: function(data){
            console.log(data)
        }
    });
});

My php file looks like this

<?php

$str = file_get_contents("test.json");

// // decode JSON
$json = json_decode($str, true);

$decodedJSON =  json_decode($str, true);
var_dump($decodedJSON);

$description = $_REQUEST;

$milliseconds = round(microtime(true) * 1000);

$description->time = $milliseconds;

file_put_contents('test.json', json_encode($description, JSON_PRETTY_PRINT));

?>

This sets the json file as

{"description":"test text entered","SQLiteManager_currentLangue":"2"}

I want the json file to look like where the number is the current time in ms.

{
    "1495134004244": {
        "images": [
            "2.JPG"
        ],
        "description": "test"
    }
}
2
  • why are you getting the contents of the json file first? are you trying to append data to whats already there? Commented May 18, 2017 at 19:13
  • @CodeGodie yes, that is why. i actually wrote this all in node and it was working but due to hosting issues i need to use php. So ideally I would figure out now how to append to the json file but was going to try and figure that out afterwards Commented May 18, 2017 at 19:16

1 Answer 1

1

You need to redo pretty much everything. I would do it in this manner:

HTML:

<form>
    <input type="text" name="description">
    <input type="hidden" name="images[]" value="1.jpg">
    <input type="hidden" name="images[]" value="2.jpg">
    <input type="submit">
</form>

JS:

$('form').submit(function(e){
    e.preventDefault();
    $.ajax({
        url: 'details.php',
        type: 'POST',
        data: $(this).serialize(),
        success: function(data){
            console.log(data)
        }
    });
});

PHP:

// get data from request
$newArray = $_REQUEST;

// get json from file
$json = file_get_contents('test.json');

// turn json into array
$masterArr = json_decode($json, true);

// get current time in milliseconds
$milliseconds = round(microtime(true) * 1000);

// use milliseconds as array key and use the new array as its value
$masterArr["$milliseconds"] = $newArray;

// turn array back to json
$json = json_encode($masterArr, JSON_PRETTY_PRINT);

// save json to file
file_put_contents('test.json', $json);

// echo the json so that you can use it in the AJAX call
echo $json;
Sign up to request clarification or add additional context in comments.

6 Comments

thanks! there is one issue. the user can upload as many images as they like.
youre welcome. In that case try something like this: w3schools.com/php/php_file_upload.asp
is it possible to remove "SQLiteManager_currentLangue": "2" from beind added to the json file?
yes its possible but I dont know where thats coming from. Are you adding anything else to the array before saving it to the json file?
@codiegodie no, not adding anything else
|

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.