Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting JavaScript Check Before Delete

Status
Not open for further replies.

jtrapat1

Programmer
Jan 14, 2001
137
US
I'm using C/CGI Programming with Linux Redhat.
I have an event handler which listens for Save, Delete, etc.
In my event handler, I want a JavaScript popup to confirm before the user Deletes a record.
Here's the Delete Code inside the event handler:
----------------------------------------------------
void event_handler() {
long sqlcode;
char *back;
// --- RESET BUTTON ---- //
if (!strcmp(evt_fld,"Reset")) {
reset_scr();
put_state();
puthtml();
}
// ---- BACK BUTTON ---- //
else if (!strcmp(evt_fld,"Back")) {
back = pop_backstack(&state);
strcpy(state.msg,"");
put_state();
exec_cgi(back);
}
// --- DELETE BUTTON --- //
else if (!strcmp(evt_fld,"Delete")) {
sqlcode = delete();
EXEC SQL COMMIT;
if (sqlcode == 0) {
back = pop_backstack(&state);
sprintf(state.msg,
&quot;<font color=\&quot;green\&quot;>%s: \&quot;%s\&quot;</font>&quot;,
&quot;Successful Deletion of Purpose Code&quot;,
state.code);
put_state();
exec_cgi(back);
}
else {
state.sqlcode = sqlcode;
sprintf(state.msg,&quot;delete() failed on code %s&quot;,state.code);
put_state();
exec_cgi(&quot;error&quot;);
}
}
-----------------------------------------------------
We're writing the html code dynamically from within a c program.
I don't know how to insert my code to submit the page.
Should I create a separate function or call the JavaScript like JavaScript: to submit the page?
Here's my function:
---------------------------------------------------------
function DeleteIt() {
if
(confirm (&quot;Are you sure you want to delete the selected record?&quot;)) {
document.forms[0].submit();
}
else {
return false
}
}
------------------------------------------------------
Thanks

John
 
As with any CGI routine that's intended to render a web page, you have to be able to create the HTML tags. You can do all of your content processing before the page is rendered or you can do it dynamically as the content needs to appear on the web page. As far as your javascript goes, insert it in the same place you would as if you were writing a static page in HTML. I've found it a lot easier, and less confusing, to use external javascript files. It makes my scriopt code a lot easier to read - especially when there's debuggging to do. There's always a better way...
 
tviman,

Thanks for the tips.
This delete confirmation will probably only appear on a couple of the pages so where would you place it?

I mean, we already have a global function library with basic javascript validation checks such as: numeric, not numeric, blanks, etc.

Should I put my delete confirm in here or keep it on the page where it is used?

Thanks

John
 
hi,
the best way to do it is using a script check in the onClick function call... where you put the delete button, use something like this

<input type='submit' value='Delete' onClick='return confirm(&quot;are you sure?&quot;);'>

if the user presses No, the onclick event handler (submit in this case..) isn't trigerred, otherwise if the user pressed yes the form is submitted normally for processing..
you can use the very same trick to the <a href='link' onClick='return confirm(&quot;are you sure?&quot;);'> too

san.
P.S. This code definitely works with IE 5.0+ (you may have to write this onclick handler as a separate function in other browsers)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top