I’ve been doing it all wrong, I used to take the value from the URI segment and didn’t realize it wasn’t the ideal way. So I changed my approach and now have everything via a $_POST. I’m not sure if I’m doing this correctly, could someone shed some light? My view contains tabular data listing items pulled from the DB. Each item has two links, “View” and “Delete.” The code seems to work but was wondering if it could be coded better. I forgot that the form name wasn’t unique, so when I went to go delete a record, it would always delete the newest record (the last hidden field was set).
myview.php (snippet)
<?php foreach($records as $record): ?>
<form method="POST" name="myform<?php echo $location->id;?>" action="/location/delete">
<a href="/location/view/<?php echo $location->id;?>">View</a> <a href="#" onclick="document.myform<?php echo $location->id;?>.submit();">Delete</a>
<br />
<input type="hidden" name="location_id" value="<?php echo $location->id;?>">
</form>
<?php endforeach ?>
For anyone who comes across this later, here’s how I solved my issue.
In my controller I have a method called
deletethat checks to see if the form field was submitted via a$_POST. If there’s no variable, redirect them somewhere with an error message. If the field was passed, then go through the normal checks to make sure the record can be deleted.