I'm having some trouble working out what's going on with my code.
I have a form containing some editable fields. There are also three radio buttons
- active (db value 1)
- inactive (db value 2)
- not found (db value 3)
The editable form opens with the radio button pre-selected based on the database value. For example, if the database value for status = 2
, the inactive
radio button is selected by default.
Sometimes the form opens and none of the radio buttons are selected.
For example;
- I open Item 2 for editing (the URL being '/inventory/edit-data.php?edit_id=2').
- The radio button is set as 'Active', fine.
- I click 'Inactive' and submit, the form does submit the data and the page refreshes with a 'Success message', fine.
- I stay on the page, select 'Not Found' and submit again I receive the following error;
Notice: Undefined index: status in C:\xampp\htdocs...\edit-data.php on line 26
I have highlighted line 26 in my code below. I'd appreciate any help or suggestions.
My (minified) code is below;
edit-data.php
if (isset($_POST['btn-update'])) {
$itemId = $_GET['edit_id'];
$status = $_POST['status']; // <---- line 26
if ($crud->update($itemId, $status)) {
$msg = "Record was updated successfully";
} else {
$msg = " While updating record!";
}
}
<form method='post'>
<table class='table table-bordered'>
<tr>
<td>ID</td>
<td>
<input type='text' name='itemId' class='form-control ' value="<?php echo $itemId; ?>" required disabled>
</td>
</tr>
<tr>
<td>Notes</td>
<td>
<input type='text' name='notes' class='form-control' value="<?php echo $notes; ?>" required>
</td>
</tr>
<tr>
<td>Status</td>
<td>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default <?php if ($status == '1' ) echo 'active btn-success' ; ?>">
<input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active
</label>
<label class="btn btn-default <?php if ($status == '2' ) echo 'active btn-warning' ; ?>">
<input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive
</label>
<label class="btn btn-default <?php if ($status == '3') echo 'active btn-danger' ; ?>">
<input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found
</label>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-update">
<span class="glyphicon glyphicon-edit"></span> Update this Record
</button>
<a href="index.php" class="btn btn-large btn-danger"><i class="glyphicon glyphicon-backward"></i> Cancel</a>
</td>
</tr>
</table>
</form>
//The (working) **javascript** to add/remove radio button classes is;
<script>
$(document).ready(function() {
$('#option1 ,#option2, #option3').change(function() {
$('#option1').parent().removeClass('btn-success').addClass("btn-default")
$('#option2').parent().removeClass('btn-warning').addClass("btn-default")
$('#option3').parent().removeClass('btn-danger').addClass("btn-default");
if ($(this).attr('id') == 'option1')
$(this).parent().removeClass("btn-default").addClass("btn-success");
else if ($(this).attr('id') == 'option2')
$(this).parent().removeClass("btn-default").addClass("btn-warning");
else
$(this).parent().removeClass("btn-default").addClass("btn-danger");
});
});
</script>
crud.php
class crud {
public function update($itemId, $status) {
try {
$stmt = $this->db->prepare("UPDATE items SET
itemId=:itemId,
status=:status
WHERE itemId=:itemId ");
$stmt->bindparam(":itemId", $itemId);
$stmt->bindparam(":status", $status);
$stmt->execute();
return true;
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}
}