How to create a JavaScript confirmation popup?

297 views
Skip to first unread message

NetAdmin

unread,
Jul 6, 2009, 4:57:35 PM7/6/09
to web2py Web Framework
Hello All,

I'm trying to display records in a table, that has a button at the end
of each row, that calls a JavaScript popup to confirm the delete.

I'm stuck and would appreciate any help anyone can offer.

Some of the code is attached.

Thanks!

Sterling

<head>
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Delete Record?")
// how can I pass the record ID from python to javascript?
// I tried something like ... recno = "{{=r.id}}"
recno = '48'
if (answer){
// delete the specified record number
alert("Deleting Record Number# " + recno )
window.location = "http://IP-ADDRESS:8000/appname/
controller/delete/" + recno;
}
else{
alert("The record was NOT deleted")
}
}
//-->
</script>
</head>

{{# tboffice.id tboffice.fdesc tboffice.fprice }}
<table>
{{ for r in recs: }}
{{ price = '%.2f' % r.fprice }}
{{ rec_id = '%04i ' % r.id }}
{{ d = r.fdesc }}
<tr>
<td> {{=rec_id}} </td> <td> {{=r.fdesc}} </td> <td align="right">
{{=price}} </td> <td> {{=LI(A("Delete", _href=URL(r=request ,
f="delete/"+str(rec_id) ))) }} </td> <td> <form> <input type="button"
onClick="confirmation()" value="Delete"> </form> </td>
</tr>
{{pass}}
</table>

Speedbird

unread,
Jul 6, 2009, 5:07:52 PM7/6/09
to web2py Web Framework
onclick="confirmation('{{=r.id}}');"

Then just accept the parameter in your JS function and handle it
there, something like

function confirmation(recno) {
...


-- Julio

NetAdmin

unread,
Jul 6, 2009, 5:21:23 PM7/6/09
to web2py Web Framework
YES! YES! YES!
That worked GREAT! I spent HOURS trying to get that correct!

Thanks So Much!

Sterling

mdipierro

unread,
Jul 6, 2009, 5:53:03 PM7/6/09
to web2py Web Framework
hey... web2py does this automatically if you use an input checkbox
with class="delete". No JS needed.

Massimo

Speedbird

unread,
Jul 6, 2009, 7:20:17 PM7/6/09
to web2py Web Framework
I must say that I grow even more impressed with this framework as time
goes by :)

Mr admin

unread,
Jul 8, 2009, 9:38:27 AM7/8/09
to web...@googlegroups.com
Thanks Massimo,

It's GREAT to know that web2py can do that, but I thought I should learn  about passing variables between python and web2py using JavaScript.
Reply all
Reply to author
Forward
0 new messages