Delete row in Sqlite database using Python Flask and jQuery
I can't get this to work. I simply need users to be able to delete rows in a table using a button. I don't want the UI to reset or scroll so I am using ajax via jQuery.
In app.py:
from flask import Flask
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
@app.route("/")
@app.route('/delete', methods=['POST'])
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.form['id'] + '"')
cur.commit()
con.close()
In index.html:
$( document ).ready(function() {
$( 'a.delete' ).click( function( e ) {
e.preventDefault();
$.ajax({
url: '/delete',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
<form action="/delete" method="post" role="form">
<input type="hidden" name="id" value="{{row['liner_ip']}}">
<a href="#" class="btn btn-danger delete">DELETE</a>
</form>
The row does not get deleted and the response code is 200. So I don't know what the issue is. I can confirm that the form gets populated properly and the ajax request is made.
Can anyone point me to a sample that does this?
python jquery flask
add a comment |
I can't get this to work. I simply need users to be able to delete rows in a table using a button. I don't want the UI to reset or scroll so I am using ajax via jQuery.
In app.py:
from flask import Flask
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
@app.route("/")
@app.route('/delete', methods=['POST'])
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.form['id'] + '"')
cur.commit()
con.close()
In index.html:
$( document ).ready(function() {
$( 'a.delete' ).click( function( e ) {
e.preventDefault();
$.ajax({
url: '/delete',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
<form action="/delete" method="post" role="form">
<input type="hidden" name="id" value="{{row['liner_ip']}}">
<a href="#" class="btn btn-danger delete">DELETE</a>
</form>
The row does not get deleted and the response code is 200. So I don't know what the issue is. I can confirm that the form gets populated properly and the ajax request is made.
Can anyone point me to a sample that does this?
python jquery flask
add a comment |
I can't get this to work. I simply need users to be able to delete rows in a table using a button. I don't want the UI to reset or scroll so I am using ajax via jQuery.
In app.py:
from flask import Flask
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
@app.route("/")
@app.route('/delete', methods=['POST'])
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.form['id'] + '"')
cur.commit()
con.close()
In index.html:
$( document ).ready(function() {
$( 'a.delete' ).click( function( e ) {
e.preventDefault();
$.ajax({
url: '/delete',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
<form action="/delete" method="post" role="form">
<input type="hidden" name="id" value="{{row['liner_ip']}}">
<a href="#" class="btn btn-danger delete">DELETE</a>
</form>
The row does not get deleted and the response code is 200. So I don't know what the issue is. I can confirm that the form gets populated properly and the ajax request is made.
Can anyone point me to a sample that does this?
python jquery flask
I can't get this to work. I simply need users to be able to delete rows in a table using a button. I don't want the UI to reset or scroll so I am using ajax via jQuery.
In app.py:
from flask import Flask
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
@app.route("/")
@app.route('/delete', methods=['POST'])
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.form['id'] + '"')
cur.commit()
con.close()
In index.html:
$( document ).ready(function() {
$( 'a.delete' ).click( function( e ) {
e.preventDefault();
$.ajax({
url: '/delete',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
<form action="/delete" method="post" role="form">
<input type="hidden" name="id" value="{{row['liner_ip']}}">
<a href="#" class="btn btn-danger delete">DELETE</a>
</form>
The row does not get deleted and the response code is 200. So I don't know what the issue is. I can confirm that the form gets populated properly and the ajax request is made.
Can anyone point me to a sample that does this?
python jquery flask
python jquery flask
asked Nov 13 '18 at 3:14
MoreScratch
78321034
78321034
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Instead of a form
, simply create a input
field. When the button is selected, the ajax
can make a GET
request:
from flask import jsonify
@app.route("/")
@app.route('/delete')
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.args.get('id')+ '"')
cur.commit()
con.close()
return flask.jsonify({'success':"True"})
Then, in the html
:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div class='wrapper'> <!--Need wrapper for anchoring button click -->
<input type="hidden" name="id" class='input_row' value="{{row['liner_ip']}}">
<button class='delete_row'>DELETE</button>
</div>
</body>
<script>
$(document).ready(function(){
$('.wrapper').on('click', '.delete_row', function(){
var val = $('.input_row').val();
$.ajax({
url: "/suggestions",
type: "get",
data: {id: val},
success: function(response) {
$('.input_row').val('');
},
});
});
});
</script>
</html>
Thanks Ajax1234, but using HTTP GET for a DELETE function is an anti-pattern. GET's should only be used for non-destructive (i.e. idempotent) operations. Any way to do this via POST?
– MoreScratch
Nov 13 '18 at 4:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273261%2fdelete-row-in-sqlite-database-using-python-flask-and-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of a form
, simply create a input
field. When the button is selected, the ajax
can make a GET
request:
from flask import jsonify
@app.route("/")
@app.route('/delete')
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.args.get('id')+ '"')
cur.commit()
con.close()
return flask.jsonify({'success':"True"})
Then, in the html
:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div class='wrapper'> <!--Need wrapper for anchoring button click -->
<input type="hidden" name="id" class='input_row' value="{{row['liner_ip']}}">
<button class='delete_row'>DELETE</button>
</div>
</body>
<script>
$(document).ready(function(){
$('.wrapper').on('click', '.delete_row', function(){
var val = $('.input_row').val();
$.ajax({
url: "/suggestions",
type: "get",
data: {id: val},
success: function(response) {
$('.input_row').val('');
},
});
});
});
</script>
</html>
Thanks Ajax1234, but using HTTP GET for a DELETE function is an anti-pattern. GET's should only be used for non-destructive (i.e. idempotent) operations. Any way to do this via POST?
– MoreScratch
Nov 13 '18 at 4:32
add a comment |
Instead of a form
, simply create a input
field. When the button is selected, the ajax
can make a GET
request:
from flask import jsonify
@app.route("/")
@app.route('/delete')
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.args.get('id')+ '"')
cur.commit()
con.close()
return flask.jsonify({'success':"True"})
Then, in the html
:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div class='wrapper'> <!--Need wrapper for anchoring button click -->
<input type="hidden" name="id" class='input_row' value="{{row['liner_ip']}}">
<button class='delete_row'>DELETE</button>
</div>
</body>
<script>
$(document).ready(function(){
$('.wrapper').on('click', '.delete_row', function(){
var val = $('.input_row').val();
$.ajax({
url: "/suggestions",
type: "get",
data: {id: val},
success: function(response) {
$('.input_row').val('');
},
});
});
});
</script>
</html>
Thanks Ajax1234, but using HTTP GET for a DELETE function is an anti-pattern. GET's should only be used for non-destructive (i.e. idempotent) operations. Any way to do this via POST?
– MoreScratch
Nov 13 '18 at 4:32
add a comment |
Instead of a form
, simply create a input
field. When the button is selected, the ajax
can make a GET
request:
from flask import jsonify
@app.route("/")
@app.route('/delete')
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.args.get('id')+ '"')
cur.commit()
con.close()
return flask.jsonify({'success':"True"})
Then, in the html
:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div class='wrapper'> <!--Need wrapper for anchoring button click -->
<input type="hidden" name="id" class='input_row' value="{{row['liner_ip']}}">
<button class='delete_row'>DELETE</button>
</div>
</body>
<script>
$(document).ready(function(){
$('.wrapper').on('click', '.delete_row', function(){
var val = $('.input_row').val();
$.ajax({
url: "/suggestions",
type: "get",
data: {id: val},
success: function(response) {
$('.input_row').val('');
},
});
});
});
</script>
</html>
Instead of a form
, simply create a input
field. When the button is selected, the ajax
can make a GET
request:
from flask import jsonify
@app.route("/")
@app.route('/delete')
def delete():
con = sqlite3.connect('ships.db')
cur = con.cursor()
cur.execute('DELETE FROM `liners` WHERE liner_ip = "' + request.args.get('id')+ '"')
cur.commit()
con.close()
return flask.jsonify({'success':"True"})
Then, in the html
:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div class='wrapper'> <!--Need wrapper for anchoring button click -->
<input type="hidden" name="id" class='input_row' value="{{row['liner_ip']}}">
<button class='delete_row'>DELETE</button>
</div>
</body>
<script>
$(document).ready(function(){
$('.wrapper').on('click', '.delete_row', function(){
var val = $('.input_row').val();
$.ajax({
url: "/suggestions",
type: "get",
data: {id: val},
success: function(response) {
$('.input_row').val('');
},
});
});
});
</script>
</html>
answered Nov 13 '18 at 3:23
Ajax1234
40.4k42653
40.4k42653
Thanks Ajax1234, but using HTTP GET for a DELETE function is an anti-pattern. GET's should only be used for non-destructive (i.e. idempotent) operations. Any way to do this via POST?
– MoreScratch
Nov 13 '18 at 4:32
add a comment |
Thanks Ajax1234, but using HTTP GET for a DELETE function is an anti-pattern. GET's should only be used for non-destructive (i.e. idempotent) operations. Any way to do this via POST?
– MoreScratch
Nov 13 '18 at 4:32
Thanks Ajax1234, but using HTTP GET for a DELETE function is an anti-pattern. GET's should only be used for non-destructive (i.e. idempotent) operations. Any way to do this via POST?
– MoreScratch
Nov 13 '18 at 4:32
Thanks Ajax1234, but using HTTP GET for a DELETE function is an anti-pattern. GET's should only be used for non-destructive (i.e. idempotent) operations. Any way to do this via POST?
– MoreScratch
Nov 13 '18 at 4:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273261%2fdelete-row-in-sqlite-database-using-python-flask-and-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown