#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    78
    Rep Power
    12

    How do I create a popup window in Javascript from Perl?


    After the user clicks a button, data will be inserted into a database, and I want a javascript alert window to pop up and say "Item successfully add". However, I cannot find a way to do this in Perl.

    I cannot make it as an "onclick" event for the button because I only want the window to pop up AFTER all the database functions are complete.
  2. #2
  3. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    83
    In the page you return (created by perl) after your database updates, stuff the relevant javascript in the "onLoad" of the <body> tag.

    If you're using HTML::Template, this is trivial.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    78
    Rep Power
    12
    I wish it could be as a simple as putting javascript into the OnLoad event, but in my situation, that wont work. When the user fills in information in the form fields and then clicks the button to submit them, I have an OnClick event for that button that reloads the SAME page. At the top, this page has an "IF" statement to check to see if cgi paramaters have been submitted (i.e. a form has been submitted). If so, it will execute the database insertion procedure. After the "IF" statement, it will recreate and display textboxes, labels, etc ( to re-display a blank form). In other words, a form will be displayed no matter what.

    So in my case, I need to execute the javascript alert window INSIDE the "IF" block because that is where I can immediately confirm to the user that data has been sucessfully added to the database.

    I need some kind of PERL function that creates and executes the "window.alert('Item successfully added')" javascript.

    Is there a way to attach an event to a database operation in PERL? For example, OnInsertion="PopUpAlert()". =)
  6. #4
  7. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    83
    Why can't you just conditionally output the onLoad event? On the first visit to the page, or any subsequent visit that doesn't meet the criteria needed to insert into the database you don't print out the alert in the onLoad.

    Upon a submit that causes you to do a database insert, you trip a flag that causes your app to output the alert into the onLoad of your body tag.

    If you're unable to get your HTML to do this, then you need to rethink how you're building your applications. Look at HTML::Template to create more flexible and maintainable apps.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    78
    Rep Power
    12
    oh, I see it now. I just realized (or actually remembered) that the OnLoad event is fired AFTER the whole page finished loading. For some reason earlier, I kept on thinking that an OnLoad event is fired when the page just BEGINS to load (before executing any statements in the body). I guess I could get it to work now. Thanks.

    Also, I partially figured out some alternatives, which may be more simple if someone can help me get it working correctly. All I need to do is write Javascript directly to the browser. However, I'm having problems using the correct content-type.

    First I did this:

    print
    (
    "Content-type: text/plain\n\n".
    "<script language='javascript' type='text/javascript'>".
    "alert('Customer successfully added.')".
    "</script>"
    );

    This almost worked perfectly (the alert box pops up and at the right time) but after the user clicks "OK" on the alert box and the page finishes loading, the phrase "Content-Type: text/html; charset=ISO-8859-1" gets displayed near the top of the screen. How do I get rid of that? I also tried changing the content-type to text/html and I get the same result.

    Then I did:

    print
    (
    "Content-type: application/x-javascript\n\n".
    "alert('Customer successfully added.')"
    );

    This time, a file download dialog box pops up and asks me if I want to open, save, etc. my perl file instead of an alert box popping up. I also change the content-type to text/javascript and I get the same download dialogue box. In fact, every content-type except the "text/html" and "text/plain" brings up a download dialog box.

    What is the correct content type that I need to use?

IMN logo majestic logo threadwatch logo seochat tools logo