Included php file calling Javascript function

Posted by Illes Peter on Stack Overflow See other posts from Stack Overflow or by Illes Peter
Published on 2010-05-11T13:44:36Z Indexed on 2010/05/12 10:24 UTC
Read the original article Hit count: 308

Filed under:
|
|

Hi there!

Here's the deal. I've got index.php which links to an internal JS file in it's header. index.php then includes another .php file, which outputs this: + add file. addFile() is a Javascript function defined in the external JS file.

By doing this nothing happens, the included php does not "see" the JS function. Encapsulating the JS in the included PHP makes it all work. But I don't want to do it that way.

Any ideas?

EDIT:

here's the source

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Archie</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" href="/screen.css" type="text/css" media="screen"/>
        <script src="/lib/js/archie.js" type="text/javascript"></script>
    </head>
    <body>
        ...
        ...
        //included php starts here
        <form action="/lib/course.php" method="post">
            <fieldset>
                    <div id="addFileLocation"></div>
                <a href="#" onClick="addFile()">+ add file</a>          
                <input type="hidden" id="addFileCount" value="0"/>
            </fieldset>
        </form>
        //ends here
        ...
        ...
    </body>
    </html>

and the js:

<script type="text/javascript">
    //Dynamically add form fields

    //add file browser
    function addFile() {
        var location = document.getElementById('addFileLocation');
        var num = document.getElementById('addFileCount');
        var newnum = (document.getElementById('addFileCount').value -1)+ 2;
        num.value = newnum;
        var newname = 'addFile_'+newnum;
        var newelement = document.createElement('input');
        newelement.setAttribute('name',newname);
        newelement.setAttribute('type','file');
        location.appendChild(newelement);
    }

</script>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about php