I have a php 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. Now I am trying to add a button in a row. That button will delete the row. Here is the script which I have used in order to add Remove button in a row.
The issue which I am having right now on the saving the form, the button doesn't stay and show up.
html/php code:
<?php
$output = array();
$output['house_sitting_date']=$_POST['house_sitting_date'];
$output['row_delete']=$_POST['row_delete'];
$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">
<!-- Add New Row Button START -->
<div class="plus-minus-button" style="text-align:center;">
<button type="button" id="addRow" onclick="rowAdd()">+</button>
</div>
<!-- Add New Row Button END -->
<div id="rows" style="display:flex; justify-content: center;"> <!-- Big div START -->
<!-- This is what I have tried to add a button (START) -->
<!-- Remove Button START -->
<div class="rows-delete" style="text-align:center;">
<h4 style="text-align:center;">Delete Rows</h4>
<?php if (empty($data->row_delete)) { ?>
<div class="row-delete" style="margin-right:30px; margin-top:20px;">
<button type="button" name="row_delete[]" value="">Remove</button>
</div>
<?php } else { ?>
<?php foreach ($data->row_delete as $row_delete){ ?>
<div class="row-delete" style="margin-right:30px; margin-top:20px;">
<button type="button" name="row_delete[]" value="<?php if($row_delete) {echo $row_delete;}?>">Remove</button>
</div>
<?php }} ?>
</div>
<!-- Remove Button END -->
<!-- This is what I have tried to add a button (END) -->
<!-- Sitting Date START -->
<div class="sitting-days" style="text-align:center;">
<h4 style="text-align:center;">Select Date</h4>
<?php if (empty($data->house_sitting_date)) { ?>
<!-- Select Date START -->
<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="house-sitting-date" name="house_sitting_date[]" value="">
</div>
<?php } else { ?>
<?php foreach ($data->house_sitting_date as $date){ ?>
<!-- Select Date START -->
<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="house-sitting-date" name="house_sitting_date[]" value="<?php if($date) {echo $date;}?>">
</div>
<!-- Select Date END -->
<?php }} ?>
</div>
<!-- Sitting Date END -->
</div> <!-- Big div END -->
</form>
<?php } else {
echo 'Cannot read JSON settings file';
}
?>
Problem Statement:
The issue which I am having right now that I can add as many rows as we want but the Remove button doesn't stay after saving the form.
I am wondering what changes I need to make in the php/javascript code above so that remove button stay after saving the form.