1

I would like to call a javascript function (included inside a html file: index.html) from a php file (xyz.php). So, when i click on a link inside the xyz.php page, it will call the javascript function from index.html.

Something like:

echo '<a href="#" onclick="index.html.someFunction(e)"></a>';

It somehow needs to adress index.html, where the function is located.

EDIT:

Index.html

<html>
 <head>    
 </head>
 <body>
<button id="search_button" type="submit" value= "Search" onclick= "return getOutput()">Search</button>

<div id="output" style="width:395px; height:150px; overflow: auto; background = #969696" >    </div>
 </body>

 <script>    


    //THIS DISPLAYS THE CONTENT OF MY PHP PAGE INSIDE THE DIV FIELD
    function getOutput() {
      getRequest(
          "xyz.php?eingabe=123&eingabe2=File.csv", // URL for the PHP file
           drawOutput,  // handle successful request
           drawError    // handle error
      );
      return false;
    }
    function getOutput(link) {
      getRequest(
          link, // URL for the PHP file
           drawOutput,  // handle successful request
           drawError    // handle error
      );
      return false;
    }

    // handles drawing an error message
    function drawError() {
        var container = document.getElementById('wagoartikelnr2');
        container.innerHTML = 'Bummer: there was an error!';
}
    // handles the response, adds the html
    function drawOutput(responseText) {
        var container = document.getElementById('wagoartikelnr2');
        container.innerHTML = responseText;
            }
    // helper function for cross-browser request object
    function getRequest(url, success, error) {
        var req = false;
        try{
            // most browsers
            req = new XMLHttpRequest();
        } catch (e){
            // IE
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                // try an older version
                try{
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    return false;
                }
            }
        }
        if (!req) return false;
        if (typeof success != 'function') success = function () {};
        if (typeof error!= 'function') error = function () {};
        req.onreadystatechange = function(){
            if(req.readyState == 4) {
                return req.status === 200 ? 
                    success(req.responseText) : error(req.status);
            }
        }
        req.open("GET", url, true);
        req.send(null);
        return req;
    }


 </script>
 </html>

XYZ.PHP

<html>
<?php

$vergleich = $_GET["eingabe"];
$datei = $_GET["eingabe2"]; 




 $temp2a = array("0.1.2.4", "0.1.2.3", "0.1.2.2");


for ($i = 0; $i < count($temp2a); $i++) {

                $URL = "xyz.php?eingabe=',temp2a[$i],'&eingabe2=',$datei,";
                echo '<a href=',temp2a[$i],' onClick= "index.html.getOutput($URL)"></a>';
            } 
?> 

<body>
</body>
</html>

2 Answers 2

3

You can save the Javascript code in an external js file and include it in your html/php files via the script tag;

<script src="path/to/your/js/file.js"></script>

EDIT: Ok, first of all, you need to remove the overloaded getOutput(link) function altogether. This will make your call to xyz.php file point to the actual php file, otherwise it points to a file called undefined (since you don't provide the link parameter). Then, there is a typo in your xyz.php file in lines 16,17:

$URL = "xyz.php?eingabe=',temp2a[$i],'&eingabe2=',$datei,";
echo '<a href=',temp2a[$i],' onClick= "index.html.getOutput($URL)"></a>';

should be changed to this;

// Note the missing $ sign from your temp2a variables and the missing quotes from the anchor tag
$URL = "xyz.php?eingabe=',$temp2a[$i],'&eingabe2=',$datei,";
echo '<a href="',$temp2a[$i],'" onClick= "getOutput($URL)"></a>';</a>';

EDIT2: Found some other minor bugs in your files;

index.html:

<html>
 <head>
 </head>
 <body>
<button id="search_button" type="submit" value= "Search" onclick= "return getOutput()">Search</button>

<div id="wagoartikelnr2" style="width:395px; height:150px; overflow: auto; background = #969696" >    </div>

<script src="script.js"></script>
 </body>
 </html>

xyz.php:

<?php

$vergleich = $_GET["eingabe"];
$datei = $_GET["eingabe2"];
$temp2a = array("0.1.2.4", "0.1.2.3", "0.1.2.2");

for ($i = 0; $i < count($temp2a); $i++) {
    $URL = "xyz.php?eingabe=".$temp2a[$i]."&eingabe2=".$datei;
    echo '<p><a href="'.$temp2a[$i].'" onClick="getOutput("'.$URL.'")">link</a></p>';
}

?>

script.js:

    function getOutput(url) {
      url = url || "xyz.php?eingabe=123&eingabe2=File.csv";
      getRequest(
          url, // URL for the PHP file
           drawOutput,  // handle successful request
           drawError    // handle error
      );
      return false;
    }

    // handles drawing an error message
    function drawError() {
        var container = document.getElementById('wagoartikelnr2');
        container.innerHTML = 'Bummer: there was an error!';
}
    // handles the response, adds the html
    function drawOutput(responseText) {
        var container = document.getElementById('wagoartikelnr2');
        container.innerHTML = responseText;
            }
    // helper function for cross-browser request object
    function getRequest(url, success, error) {
        var req = false;
        try{
            // most browsers
            req = new XMLHttpRequest();
        } catch (e){
            // IE
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                // try an older version
                try{
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    return false;
                }
            }
        }
        if (!req) return false;
        if (typeof success != 'function') success = function () {};
        if (typeof error!= 'function') error = function () {};
        req.onreadystatechange = function(){
            if(req.readyState == 4) {
                return req.status === 200 ?
                    success(req.responseText) : error(req.status);
            }
        }
        req.open("GET", url, true);
        req.send(null);
        return req;
    }
Sign up to request clarification or add additional context in comments.

8 Comments

Do i have to change some things inside the javascript after i saved it in an external file ? Because some things refer to html-elements inside the index.html
Usually you don't. Just make sure you place the script tag at the bottom of your pages (right before the body closing tag).
After i have exported my JavaScript into an external file it does not work anymore as it used to. I used this method (stackoverflow.com/questions/7165395/…) to output the content of my php page inside my html page, but now I only get tne error message from drawError()
If there is inline PHP code in your Javascript then I am afraid you will just have to copy the javascript code in your PHP files. It would help a lot if you could add the relevant code parts to your question.
I will add both of my files, wait a second
|
0

use this code

<script type="text/javascript" src="path_to_ur_filename.js"></script> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.