PHP. How to get $_POST value after table disappears from HTML

PHP. How to get $_POST value after table disappears from HTML

Problem Description:

I am sorry for the title. That was my best shot to explain the situation with the least words.

I am trying to make a php program that has a html part with select and option. I am not using ajax or mysql. Just using a JSON file and xampp for apache.

if you select one of the options,`


this php code will work in the html, and show a series of input boxes where you can type in what ever you want. Each option has an array within a JSON file.
So, I have put it in


and iterated it with a key => value. and shoved it in the input box. The value of the input box would be initially the value of the JSON file I called. I wanted the user to erase that text and type in their own. There could be several input boxes depending on the choice the user makes.

The name of the input box would be the KEY.

Then if you hit the edit button which is a input type submit, the series of input boxes will disappear.

I wanted to get the return with a

But, whatever I choose, the $_POST[KEY] will just return me the very first option of the select option html.

IS there a way I can solve this?

I need to get the corresponding array of the selected choice.

My goal is to get the values of the input box and update a JSON file.

<select name = "muscle">
    <option value = "chest">Chest</option>
    <option value = "back">Back</option>
    <option value = "leg">Leg</option>
<input type="submit" name="choice" value="choose">
<?php if(isset($_POST["choice"])) : ?>
<h3> Current Workout Program </h6>
foreach ($program[$_POST["muscle"]] as $key => $val):    
<p><?= $key. ":" . $val;?></p>
<input type="text" name="<?=$key?>" value="<?=$val?>">
<?php endforeach;?>
<input type="submit" name="edit" value="edit">

<?php endif;?>

The iteration of the Key value above works fine.

But if I do a

if (isset($_POST["edit"])){


After submission, It will give me the array for "chest" only.

Solution – 1

As I understood your code, if you submit the mentioned form the $_POST would be as the following box:

//$_POST is like the following
    "muscle" => "chest",
    "choice" => "choose"

So in the result page, if(isset($_POST["choice"])) condition check would be always true, And the progress will go right.

I think on the destination page (eg. edit page) you have to add a hidden input as the following to make sure you tell the system which muscle you are editing:

<input type="hidden" name="muscle" value="<?= $_POST['muscle'] ?>">

Please note that the input type is hidden and it’s not shown to the person who is working with the form.

Check the solution out and let me know if there are any other issues with the response.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.