I have following table:
<form action="update-table.php" method="post">
<table class="table-data" border="1">
<thead>
<tr>
<td>ID</td>
<td>Live</td>
<td>User</td>
<td>Last Update</td>
<td>Title</td>
<td>Content</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</thead>
<tbody>
<tr>
<td><input name="id[]" type="text" readonly="readonly" value="1" /></td>
<td><input name="live[]" type="checkbox" checked="checked" value="1"/></td>
<td>admin@website.com</td>
<td>August 18, 2011, 10:10 am</td>
<td>Title1</td>
<td>Content1</td>
<td><a href="edit-news.php?editID=1">Edit</a></td>
<td><a href="delete-news.php?deleteID=1">Delete</a></td>
</tr>
<tr>
<td><input name="id[]" type="text" readonly="readonly" value="2" /></td>
<td><input name="live[]" type="checkbox" /></td>
<td>admin@website.com</td>
<td>August 18, 2011, 10:11 am</td>
<td>Title2</td>
<td>Content2</td>
<td><a href="edit-news.php?editID=2">Edit</a></td>
<td><a href="delete-news.php?deleteID=2">Delete</a></td>
</tr>
<tr>
<td><input name="id[]" type="text" readonly="readonly" value="3" /></td>
<td><input name="live[]" type="checkbox"/></td>
<td>admin@website.com</td>
<td>August 18, 2011, 10:10 am</td>
<td>Title3</td>
<td>Content3</td>
<td><a href="edit-news.php?editID=3">Edit</a></td>
<td><a href="delete-news.php?deleteID=3">Delete</a></td>
</tr>
</tbody>
</table>
<input type="submit" />
What I’m trying to do is display the array send with $_POST.
The problem is when checkbox is not checked – nothing is being send.
How to check if the $_POST['live'] is set, and if not – set it’s value to 0?
The following code does not work. Why?
if (!isset($_POST['live'])) {
$_POST['live'] = "0";
}
for ($i=0;$i<count($_POST['id']);$i++) {
echo "<p>".$_POST['id'][$i]."</p>";
echo "<p>".$_POST['live'][$i]."</p>";
echo "<hr />";
}
Try this: