Reputation: 35
Hi There I have a form where a user can enter some information into forms, which is then stored in a database (posts.sql) All of the previous rows in a table in this database are displayed below this form:
See here: https://i.sstatic.net/EPVOb.png
I am wondering how I could uniquely identify each row's "delete" and "update" button, as the buttons are not parts of the array itself; but just generate in a foreach loop.
Like so
@foreach ($posts as $post)
<tr>
<td>{{{ $post->id }}}</td>
<td>{{{ $post->post_username }}}</td>
<td>{{{ $post->post_title }}}</td>
<td>{{{ $post->post_message }}}</td>
<td><input type="button" onclick="alert('Post Deleted')" value="Delete"></td>
<td><input type="submit" value="Update"></td>
</tr>
@endforeach
and then perform this function to delete the post.
function delete_post($id)
{
$sql = "delete from posts where id = ?";
DB::delete($sql, array($id));
}
and display a new page with the following form where I can update a single row. using a form similar to the one below.
<form method="post" action="{{{ url('update_post_action') }}}">
<input type="hidden" name="id" value="{{{ $post->id }}}">
<table>
<tr>
<td>Post Message</td>
<td><input type="text" name="post_message"value="{{{ $post->post_message }}}"></td>
</tr>
<tr>
<td colspan=2><input type="submit" value="Update item"></td>
</tr>
</table>
</form>
Upvotes: 1
Views: 1775
Reputation: 35
Hihi I was able to do this by adding an action to a form within the table and then getting the value of the submit button as a variable in the routes.php file.
home.blade.php
<td> <!-- Creates a New #Swaggy Button that links to routes to delete item -->
<form method="post" action="{{{ url('delete_post_action') }}}">
<input type="hidden" name="delete_id" value="{{{ $post->id }}}">
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</td>
routes.php
Route::post('delete_post_action', function()
{ /* Get post->id from hidden input then delete row */
$id = Input::get('delete_id');
$sql = "delete from posts where id = ?";
DB::delete($sql, array($id));
/* Rebuild webpage */
$sql = "select * from posts ORDER BY ID DESC";
$current_posts = DB::select($sql);
$results=$current_posts;
return View::make('pages.home')->withposts($results);
});
Upvotes: 0
Reputation: 1697
If you are using CRUD routes you will need a form with DELETE action for delete. Update is a simple url like:
<a href="posts/{!! $post->id !!}/edit">edit</a>
And you can use css to see it like a button.
You can add a form for each row (and action) if you like
I give you a sample with link with javascript submit and button submit sample for post delete.
@foreach ($posts as $post)
<tr>
<td>{{{ $post->id }}}</td>
<td>{{{ $post->post_username }}}</td>
<td>{{{ $post->post_title }}}</td>
<td>{{{ $post->post_message }}}</td>
<td>
{!! Form::open(array('method' => 'DELETE', 'route' => array('posts.destroy', $post->id), 'id'=>'post_'.$post->id)) !!}
<a href="javascript:$('#post_{{$item->id}}').submit();">delete link</a>
<input type="submit" value="Delete button">
{!! Form::close() !!}</td>
<td><input type="submit" value="Update"></td>
</tr>
@endforeach
Upvotes: 0
Reputation: 8223
If you could change your button to this:
<button name="delete" value="{{ $post->id }}">Delete</button>
then you could use
if (isset($_POST['delete']))
{
delete_post($_POST['delete']);
}
Upvotes: 2