#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    3
    Rep Power
    0

    Question Can you call a javascript function from code within a perl script?


    Hello all,

    I have a quick question. Basically I have a perl script that defines a form field in an HTML file that is reffered to by the perl script. When accessed the script opens the html file and it displays the form data defined in the perl script through the use of a variable (eg:$page).

    I have an onclick even in the form that I want to call a function which is defined in the html file.. How do I get it to work.

    Quick example of what I mean...

    PEARL SCRIPT

    &freate_form;
    &Print_Details_Page;

    sub create_form{
    $pagetest = ""
    $pagetest .= "<form name=COURSES action=\"craft2.html\">";
    $pagetest .= "<input type=\"checkbox\" name=\"test\" onclick= TEST()></FORM>" ;
    }

    sub Print_Details_Page {
    #Create a confirmation page
    open (PAGE, "<test.html");
    @page= <PAGE>;
    close PAGE;
    print "content-type: text/html\n\n";
    &Print_Web_Page;
    }



    HTML FILE (TEST.html)
    <HTML>
    <SCRIPT> Here I'd make the function 'test'</SCRIPT>

    $pagetest
    </HTML>

    So can I call the function test from within the perl script that defines the page even though the function is not defined in the script but rather the html?

    Thank you in advance for any help you may be able to provide.
  2. #2
  3. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0
    Huh? Do you mean you want to call a perl function from an HTML page?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191

    Thumbs down nope, sorry....


    sorry, "el presidente", both javascript and perl work completely different from what you are trying (unless you use client-side perl which is not supported by any browser that i know of without major tweaking... ). to me it seems like you know perl, but you never worked with javascript before, is this true?

    to answer your question:
    yes, what you described is somewhat possible. but not in your approach. PERL is not really necessary for onclick functions, since it is server-side.

    you can do this:
    Code:
    <script language="JavaScript">
    function TEST() {
    ...
    }
    </script>
    ...
    ...
    <input type="checkbox" name="test" onclick= TEST()>" ;
    and all this code can be the output of PERL.

    or you could do this:
    Code:
    <form action="myscript.pl?func=test">
    ...
    </form>
    
    and in the perl script:
    
    if (CGI->GET("func") eq "test") {
      ... test function
    }
    where you need to find the right substitute for CGI->GET to read the GET-variables (sorry, i am not used to NET::CGI or any module like that...)
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    3
    Rep Power
    0
    Thank you for the assisstance but it would seem my code was already working but it was an error in the javascript that was preventing the function from delivering the expected results.

    As for the question re use of Javascript and Pearl I have used both with probably a bit more experience with JS... This is the first time I've had to combine the two for server and client side functionality.

    The reason the onclick event is occuring from within Pearl is because the check box and other from object values are being generated from a text based DB read by the pearl script. For each line of the database a new table cell is created. Since the number of checkboxes will depend on the number of entries in the database it is impossible to put the html for the form outside of Perl since I need it to create the form to correct specifications dictated by the file itself. So in truth the perl script is merely reading the database and writing the html so in reality the onclick even IS being called from the html page, it is just defined in a perl script.

    Anyhoo thanks once again but I managed to answer my own question. Yes I can call a JS function when the html code is produced by Pearl.

IMN logo majestic logo threadwatch logo seochat tools logo