0

Im trying to delete rows from an sqlite database and then refresh the page. This should all happen when i click the button(shown bellow).

Button:

<button type="button" class="btn btn-danger" onclick= "{{{ url("delete_item_action/$post->Id") }}}">Delete</button>

Here are the functions that should execute the sql query delete:

function delete_Post($id)
{
 $sql = "delete FROM Post WHERE id = ?";
 DB::delete($sql, array($id));
} 
function delete_item_action($id)
{
  delete_Post($id);
  return View::make('social.home');
}
1
  • And what is your question? Commented Apr 15, 2015 at 6:36

2 Answers 2

1

This won't work because the onclick parameter is a javascript function, which the browser tries to execute but since there is only a url there, it will probably fail with an "undefined" error.

You have multiple ways to achieve what you want:

  1. Make a form

<form action="{{{ url('your url')}}}">
        <input type="submit" class="btn btn-danger">Delete</input>
</form>

  1. Make an anchor

(It's not tested, don't know if this will work)

<a href="{{{ url('your-url') }}}"><button>...</button></a>

  1. Call a javascript function which changes the current location

<button ... onclick="window.location.href={{{ url('your-url') }}}">...</button> 

Sign up to request clarification or add additional context in comments.

Comments

0

Or you can use jquery and ajax to submit your request asynchronously:

<script src="jquery-2.1.3.js"></script>
<script>
function delete_post( id ) {
  console.log('firing delete post');
  $.ajax({
    url: 'delete_onclick/'+id,
    method: 'post',
  }).done( function( data ) {
    alert(data);
  });
}
</script>
<button type="button" class="btn btn-danger" onclick="delete_post(2)">Delete</button>

And your routes

Route::get('onclick', function() { return View::make('onclick'); });
Route::post('delete_onclick/{id}', function($id) {
  // Post::find($id)->delete(); or something similar
  return "Deleted entry";
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.