Reputation: 21
I wanted to get result like :
[{"status":"open","open_time":"0900","close_time":"2100"},
{"status":"open","open_time":"0730","close_time":"2100"},
{"status":"open","open_time":"0730","close_time":"2100"},
{"status":"open","open_time":"0730","close_time":"2100"},
{"status":"open","open_time":"0730","close_time":"2100"},
{"status":"open","open_time":"0730","close_time":"2100"},
{"status":"open","open_time":"0900","close_time":"2100"}]
this using json.
Here is my php form:
<div class='form_field'>
<?php
echo " <table> <tr><th> </th><th> </th><th>Opening</th><th>Closing</th></tr>";
$floatroom_hours =array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
for($counter = 0; $counter < 7; $counter++)
{ echo "<tr><td>$floatroom_hours[$counter]</td>";
echo "<td><input type='checkbox' name='status' </td>";
if (isset($_POST['status'])) "value='open'";
else "value='closed'";
echo "<td><input type = 'text' name = 'open_time' class = 'hours' size='4' maxlength='4' /></td>";
echo "<td><input type = 'text' name = 'close_time' class = 'hours' size='4' maxlength='4' /><br/></td></tr>";
$hours['status'] = $_POST['status'];
$hours['open_time'] = $_POST['open_time'];
$hours['open_time'] = $_POST['close_time'];
}
echo "</table>";
?>
<?php
echo form_submit(array(json_encode($hours)=>$floatroom_info->hours));?>
</div>
However I am not getting desired result.
I need some help on this. Can someone share your experience please?
Upvotes: 0
Views: 82
Reputation: 15643
First things first, your HTML is wrong to start with. At this moment your only posting one open_time
, close_time
and status
The HTML should be changed first to this :
for($counter = 0; $counter < 7; $counter++) {
echo '<tr>';
echo ' <td>'.$floatroom_hours[$counter].'</td>';
echo ' <td><input type="checkbox" name="status['.$counter.']"'. (isset($_POST['status'][$counter]) ? ' checked' : '') . ' /></td>';
echo ' <td><input type="text" name = "open_time['.$counter.']" class = "hours" size="4" maxlength="4" /></td>';
echo ' <td><input type="text"name = "close_time['.$counter.']" class = "hours" size="4" maxlength="4" /><br/></td>';
}
As you see the input will now be posted as an array. Next step is processing the form. Best thing here is to move the logic outside the form. Place this snippet just under the PHP tag
<?php
if (!empty($_POST)) {
if (isset($_POST['open_time'])) {
$data = array();
foreach($_POST['open_time'] as $index => $value) {
$data[] = array(
'status' => isset($_POST['status'][$index]) ? 'open': 'closed',
'open_time' => $_POST['open_time'][$index],
'close_time' => $_POST['open_time'][$index],
);
}
echo json_encode($data);
}
}
Upvotes: 1