I am working on the following code in order to pick checkboxes from a form. If i check the first checkbox everything works great. If i check another checkbox i get the "Undefined index" error when sending bulkcopy
form. Keep in mind that i am getting the checkboxes with post method and the submit button is above the checkboxes due to the complexity of the location of the form and the fields. What i need essentially is to pick multiple checkboxes and add certain values to the database.
<?php //bulkcopy.php
session_start();
if($_SESSION['admin_logged_in'] != true){
header("Location:login.html");
exit();
}
include 'db.php';
$from = mysql_real_escape_string($_GET['from']);
$room = mysql_real_escape_string($_POST['room']);
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $check) {
$id = $check;
$sel = mysql_query("select * from $from where id = '$id' limit 1 ") or die(mysql_error());
while($row = mysql_fetch_array($sel)){
$preview = $row['preview'];
$text = $row['text'];
$title = $row['title'];
$images = $row['images'];
}
$ins = mysql_query("insert into $room (id, preview, text, title, images) values (' ', '$preview', '$text', '$title', '$images') ") or die(mysql_error());
}
header("Location:admin.php");
}
?>
The code of the form can be found below:
<form class="form-inline" name="bulkcopy" method="post" action="bulkcopy.php?from=sights"> <b>Bulk Copy:</b>
<select name='room' class="form-control">
<option>Select...</option>
<option value="Orhan">Orhan</option>
<option value="Deniz">Deniz</option>
<option value="Irini">Irini</option>
<option value="Katina">Katina</option>
<option value="Gulbin">Gulbin</option>
<option value="Mihalis">Mihalis</option>
</select>
<input class="btn btn-primary" type="submit" name="submit" value="Go"><br /><br />
</div>
<table class="table table-bordered table-striped">
<th>Entry Name</th>
<th>Display Order</th>
<th>Copy to...</th>
<th>Status</th>
<th>Image</th>
<th>Edit</th>
<th>Delete</th>
<th>Duplicate</th>
<?php while($row = mysql_fetch_array($sel)) { ?>
<tr>
<td>
<input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>">
</form>
<?php echo $row['title']; ?>
</td>
<td>
<form name="order" method="post" action="sightorder.php?id=<?php echo htmlspecialchars($row['id']); ?>">
<div class="col-md-4">
<input class="form-control" type="number" name="order" value="<?php echo htmlspecialchars($row['ordernum']); ?>">
</div>
<div class="col-sm-3">
<input type="submit" name="submit" value="Set Order" class="btn btn-primary">
</div>
</form>
</td>
<td>
<form name="copyto" method="post" action="copyto.php?from=sights&id=<?php echo htmlspecialchars($row['id']); ?>">
<input type="checkbox" name="room[]" value="Orhan"> O -
<input type="checkbox" name="room[]" value="Deniz"> D -
<input type="checkbox" name="room[]" value="Irini"> I -
<input type="checkbox" name="room[]" value="Katina"> K -
<input type="checkbox" name="room[]" value="Gulbin"> G -
<input type="checkbox" name="room[]" value="Mihalis"> M
<input type="submit" name="submit" value="Copy" class="btn btn-primary">
</form>
</td>
<td>
<a href="sightstatus.php?id=<?php echo htmlspecialchars($row['id']); ?>&status=<?php echo $row['status']; ?>"><?php if($row['status'] == 1){ ?><i class="fa fa-check fa-lg"></i><?php }else{ ?><i class="fa fa-times fa-lg"></i><?php } ?></a>
</td>
<td>
<a href="sightimages.php?id=<?php echo $row['id']; ?>"><i class="fa fa-image fa-lg"></i></a>
</td>
<td>
<a href="editsight.php?id=<?php echo htmlspecialchars($row['id']); ?>"><i class="fa fa-edit fa-lg"></i></a>
</td>
<td>
<a onclick="return confirmDelete()" href="delsight.php?id=<?php echo htmlspecialchars($row['id']); ?>"><i class="fa fa-trash fa-lg"></i></a>
</td>
<td>
<a href="duplicatesight.php?id=<?php echo htmlspecialchars($row['id']); ?>"><i class="fa fa-copy fa-lg"></i></a>
</td>
</tr>
<?php } ?>
</table>
Any help would be greatly appreciated. Thanks.
You have a problem here
<?php
while($row = mysql_fetch_array($sel)){ ?>
<tr><td><input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>"> <?php echo $row['title']; ?></td></form>
There is no closing bracket for the while loop, and the form is closed after the first checkbox is added. So if that checkbox is not checked, then the input is not posted, thus the undefined index. Make sure you do not close the form until after all the rows have been added, like this
<?php
while($row = mysql_fetch_array($sel)){ ?>
<tr><td><input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>"> <?php echo $row['title']; ?></td></tr>
<?php } ?>
</table>
</form>
After reviewing the raw HTML of the complete page you provided, it is clear that the problem is you're trying to nest multiple forms which is invalid HTML. Please refer to this answer for more information. This answer does link to a workaround, but it's an ugly hack and should probably be avoided.
I believe the appropriate, valid HTML solution in your case is to use a single form. Currently you have multiple nested forms submitting to the following locations:
What you can do is have a single form that determines which action to take based on which submit button was clicked. For example:
switch ($_POST['submit']) {
case 'Go':
// process bulkcopy
break;
case 'Set Order':
// process siteorder
break;
// etc...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With