0

I want to post unlimited form data to a JSON file in php. When I do this:

$vals = array(
      array(
          'quest' => $_POST['question'],
          'a' => $_POST['answer_a'],
          'b' => $_POST['answer_b'],
          'c' => $_POST['answer_c'],
          'd' => $_POST['answer_d'],
          'correct' => $_POST['correct_answer']
        )
      );

the result for on post is:

[
{
    "quest": "HI?",
    "a": "no?",
    "b": "yes?",
    "c": "Maybe?",
    "d": "Nah...",
    "correct": "D"
}
]

But when I add multiple posts the result is:

[
{
    "quest": "HI?",
    "a": "no?",
    "b": "yes?",
    "c": "Maybe?",
    "d": "Nah...",
    "correct": "D"
}
][
{
    "quest": "HI?",
    "a": "no?",
    "b": "yes?",
    "c": "Maybe?",
    "d": "Nah...",
    "correct": "D"
}
]

How can I format the json data correctly on each post submit?

6
  • okay show me the full code... You are getting same data in every array na...? Commented Feb 1, 2018 at 3:43
  • Im purposely posting the same data. Its just not formatting correctly. I cant post the full code... Commented Feb 1, 2018 at 3:54
  • where is the code...? Commented Feb 1, 2018 at 3:59
  • $vals[] = array( 'quest' => $_POST['question'], 'a' => $_POST['answer_a'], 'b' => $_POST['answer_b'], 'c' => $_POST['answer_c'], 'd' => $_POST['answer_d'], 'correct' => $_POST['correct_answer'] ); $enC = json_encode($vals, JSON_PRETTY_PRINT); if (!file_exists('tempQuestions.json')) { file_put_contents('tempQuestions.json', $enC, FILE_APPEND); } else { $vals[] = file_get_contents('tempQuestions.json'); } Commented Feb 1, 2018 at 4:08
  • Show me the html too Commented Feb 1, 2018 at 4:29

1 Answer 1

1

I SOLVED IT!

if (!file_exists('tempQuestions.json')) {
    $vals[] = array(
        'quest' => $_POST['question'],
        'a' => $_POST['answer_a'],
        'b' => $_POST['answer_b'],
        'c' => $_POST['answer_c'],
        'd' => $_POST['answer_d'],
        'correct' => $_POST['correct_answer']
    );

    $enC = json_encode($vals, JSON_PRETTY_PRINT);
    file_put_contents('tempQuestions.json', $enC, FILE_APPEND);

} else {

    $newz = array();

    $vals[] = array(
    'quest' => $_POST['question'],
    'a' => $_POST['answer_a'],
    'b' => $_POST['answer_b'],
    'c' => $_POST['answer_c'],
    'd' => $_POST['answer_d'],
    'correct' => $_POST['correct_answer']
    );
    $new = file_get_contents('tempQuestions.json');
    $newz = json_decode($new);
    $valz = array_merge($vals, $newz);
    $fp = fopen('tempQuestions.json', 'w');
    $enC = json_encode($valz, JSON_PRETTY_PRINT);
    file_put_contents('tempQuestions.json', $enC, FILE_APPEND);
}
Sign up to request clarification or add additional context in comments.

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.