0

I have some basic code that takes results from a form in a previous page, and encodes it to a text file. For some reason, it encodes it properly, then makes a second array that just has null values.

(There are the same amount of values in each array)

I honestly have no idea what's causing this.

Here is the encode code:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$destination = $_POST['destination'];
$msg = $_POST['msg'];

//TODO the file write here VV, use 'a' instead of 'w' too ADD to the file instead of REWRITING IT.
$arr = [$name,$email,$date,$destination,$msg];

$write = json_encode($arr);
echo $write;

$file = fopen('data.txt', 'a');
fwrite($file, $write);
fclose($file);
// echo $arr[];
 ?>

Here is the result in the .txt file:

["Simon","[email protected]","0101-01-01T01:01","Ohio","Message here"][null,null,null,null,null]

(It writes them on the same line, if that helps)

I don't want this null array here, as it will mess up some of the things I need to do. Any thoughts?

2
  • If this page is called with no values, it will write nulls - try adding if ( isset($_POST['name'])) { // all your current code } to only process when there is data. Commented May 19, 2019 at 7:52
  • Please don't include tags that are unrelated to your problem itself. I've removed the atom-editor tag, it's irrelevant to your problem. Commented May 19, 2019 at 18:19

2 Answers 2

1

The issue is not with json_encode. You are receiving two separate POST requests - one with proper data, and the subsequent ones without any data, which gets appended to the file as null values. You'll need to debug at the client why two requests are being sent

Sign up to request clarification or add additional context in comments.

3 Comments

To make sure browser sends two requests - open network tab by hitting F12 and see if it's submitting two requests. Difficult to say without seeing front-end code. May be you have an event listener on the submit event that sends the additional request?
I still have no idea why it's sending two, and there's nothing attached to the submit.
The second request might not be a POST request, there's nothing to test for the request method in the code.
0

You need to check that the post values are defined:

<?php
if (isset($_POST['name'])
        && isset($_POST['email'])
        && isset($_POST['date'])
        && isset($_POST['destination'])
        && isset($_POST['msg'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $date = $_POST['date'];
    $destination = $_POST['destination'];
    $msg = $_POST['msg'];

    //TODO the file write here VV, use 'a' instead of 'w' too ADD to the file instead of REWRITING IT.
    $arr = [$name,$email,$date,$destination,$msg];

    $write = json_encode($arr);
    var_dump( $write);
    $file = fopen('data.txt', 'a');
    fwrite($file, $write);
    fclose($file);
    // echo $arr[];
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.