Skip to content
Advertisement

input name[] to store values in html/php

I have a php/html code as shown below. The below html/php code is working in a way that on adding rows, we can select date from every row and can save it as well.

Here is the script which I have used in order to add a row.

<?php
$output = array();     
$output['house_sitting_date']=$_POST['house_sitting_date'];

$fp = fopen('../feeds/ptp-ess_landing_house.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);

if(file_exists('../feeds/ptp-ess_landing_house.json')){
    $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_house.json'));
}
?>

<?php if($data) { ?>
<form method="post">
        <!-- Select Date START -->
        <div class="select-date" style="margin-right:30px;">
            <h4 style="text-align:center;">Select Date</h4>
            <input type="date" id="house-sitting-date" name="house_sitting_date[]" value="<?php if($data->house_sitting_date<>''){echo $data->house_sitting_date;}?>">
        </div>
        <!-- Select Date END -->
 </form>
<?php } else {
echo 'Cannot read JSON settings file';
}
?>

Problem Statement:

I am wondering what changes I should make in the php code above so that when we add a row and then after selecting date in every row, everything should get saved in the JSON.

At this moment, nothing is getting saved in the JSON (../feeds/ptp-ess_landing_house.json) after saving the form.


What I want to achieve is after saving the form, it should display the selected date from each individual row we have added.

Advertisement

Answer

Make the house_sitting_date property an array of dates. Then you can loop over them when creating the form, and write the entire array back into the JSON file when the form is submitted.

<?php     
$output['house_sitting_date']=$_POST['house_sitting_date'];

if(file_exists('../feeds/ptp-ess_landing_house.json')){
    $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_house.json'));
}
?>

<?php if($data) { ?>
    <form method="post">
        <?php if (empty($data->house_sitting_date)) { ?>
            <!-- Select Date START -->
            <div class="select-date" style="margin-right:30px;">
                <h4 style="text-align:center;">Select Date</h4>
                <input type="date" class="house-sitting-date" name="house_sitting_date[]" value="">
            </div>
            <!-- Select Date END -->
        <?php } else {
                foreach ($data->house_sitting_date as $date) { ?>
            <!-- Select Date START -->
            <div class="select-date" style="margin-right:30px;">
                <h4 style="text-align:center;">Select Date</h4>
                <input type="date" class="house-sitting-date" name="house_sitting_date[]" value="<?php if($date)){echo $date;}?>">
            </div>
            <!-- Select Date END -->
        <?php } ?>
    </form>
    <?php
    if (isset($_POST['house_sitting_date'])) {
        $data->house_sitting_date = $_POST['house_sitting_date'];
        file_put_contents('../feeds/ptp-ess_landing_house.json', json_encode($data));
        echo "JSON file updated";
    }
} else {
    echo 'Cannot read JSON settings file';
}

Also, you should not use the same ID on repeated elements, IDs are supposed to be unique. Use class="house-sitting-date" instead of id="house-sitting-date" if you need to target it with CSS or JavaScript.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement