-1

Yesterday I was searching the whole day for solutions but I'm still stuck

This is my problem:

I have a PHP page where I have to fill out a form with 3 values, when I click submit it should create a new JSON array in a JSON array, with the number of the array incremented by 1.

What I want to achieve:

When I fill in the form 5 times and click the submit button 5 times, my JSON array should look like this:

{
  "articles": {
    "1": {
      "tax": "11",
      "price": "111",
      "discription": "Transport"
    },
    "2": {
      "tax": "234",
      "price": "4532",
      "discription": "Opslag"
    },
    "3": {
      "tax": "19",
      "price": "19",
      "discription": "Gasoline"
    },
    "4": {
      "tax": "84765",
      "price": "4235",
      "discription": "Food"
    },
    "5": {
      "tax": "132",
      "price": "5460",
      "discription": "Opslag"
    }
  }
}

What I tried to do:

I tried to create something in javascript: it creates an JSON Array from all input field but I cannot store the fields and make a Multidimensional array out of the result so far. What I did find was an interesting Stack Overflow question Click but I have no idea how I could use input field to populate the array like the example on the question.

I hope someone can help me :)

Thomas

6
  • Have you tried any code? As it's not easy to guess what exactly you trying to achieve. Commented Aug 24, 2018 at 7:04
  • Do you need this serverside or clientside? I tried to create something in javascript but with no success. Okay, what did you try? Please post an MCVE so we can really help solving the issues you encounter. Commented Aug 24, 2018 at 7:04
  • @Loek I need this serverside Commented Aug 24, 2018 at 7:09
  • We need to see your code Commented Aug 24, 2018 at 7:09
  • @Joseph_J Which code? Commented Aug 24, 2018 at 7:23

2 Answers 2

1

You can do this two ways. Both are suitable. I think I would consider the second way more secure as it is done completely server side. I will demo both for you.

First way:

You are going to use a hidden input field and serialize an array. You will pass the serialized array back to your post array on submit via the hidden input field. The code will push the new post data onto the unserialized array that it got from the hidden input field.

Like so:

<?php

if(isset($_POST['submit']) && $_POST['submit']){

    $array = unserialize(base64_decode($_POST['articles']));

    $array['articles'][] = array(

        'tax'               => $_POST['tax'],
        'price'             => $_POST['price'],
        'description'       => $_POST['description']

    );

    $postData = base64_encode(serialize($array));

}

?>

<!DOCTYPE HTML>
<html>

  <head>
    <title>My Simple Form</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
  </head>

  <body style="background-color:#b3ffff">

        <div style="padding-left:500px; padding-top:200px">

            <form action=""  method="post" enctype="multipart/form-data">

                Tax: <input type="text" name="tax" placeholder="tax" value=""><br>
                Price: <input type="text" name="price" placeholder="price" value=""><br>
                Description <input type="text" name="description" placeholder="description" value=""><br>
                <input type="hidden" name="articles" value=" <?php echo $postData; ?> ">
                <input type="submit" name="submit" value="Submit">

            </form>

    </div>

  </body>

</html>


<?php

echo
'<pre>';
print_r($array);
echo
'</pre>';

 //When you need to convert it to a json string then use this:
 $jsonString = json_encode($array);

?>

The second way

This way does not use a hidden input field. Instead we are just going to pass the post data to a $_SESSION variable which will store the array in memory on the server side. Just make sure you delete the session variable when you decide to leave the page because it will always be there if you don't. What I mean by that is that if you reload the page at a later time, all the data from the first time you were on the page will still be there.

session_start();

if(isset($_POST['submit']) && $_POST['submit']){

    $_SESSION['myData']['articles'][] = array(

        'tax'               => $_POST['tax'],
        'price'             => $_POST['price'],
        'description'       => $_POST['description']

    );

}

?>

<!DOCTYPE HTML>
<html>

  <head>
    <title>My Simple Form</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
  </head>

  <body style="background-color:#b3ffff">

        <div style="padding-left:500px; padding-top:200px">

            <form action=""  method="post" enctype="multipart/form-data">

                Tax: <input type="text" name="tax" placeholder="tax" value=""><br>
                Price: <input type="text" name="price" placeholder="price" value=""><br>
                Description <input type="text" name="description" placeholder="description" value=""><br>
                <input type="submit" name="submit" value="Submit">

            </form>

    </div>

  </body>

</html>


<?php

echo
'<pre>';
print_r($_SESSION['myData']);
echo
'</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

Just finished an alternative with a new table and stuff, that also works for me I definitely going to use your example, thank you so much :)
Cool, I hope the rest of your project goes well! ~Cheers
0
$array[] = $_POST["value"]; // push array after submit

$strJSON = json_encode($array);    //convert Array to JSONString
$objJSON = json_decode($strJSON);  //convert JSONString to JSONObject

1 Comment

Can you explain further why this helps? To me, that looks very strange....

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.