4
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by Biiim
0
<script type='text/javascript'>
    function doSomething(){
        window.location = 'http://www.google.co.uk/';
    }
</script>

<?php
echo '<a href="javascript:" onclick="doSomething();">Do Something</a>';
?>

Edited by Biiim: script taken out

0

I have another question. I already have function doSomething($v1,$v2) written in PHP not in Javascript.

Is it possible to call PHP functions instead of javascript functions? How to do that? Thanks

Edited by 3tplus: correct

0

PHP is server side language while javascript is client side. You can not combine both.
If you want to use php functionality in javascript you can use ajax which will send js data to php and will return with php output.

What is your exact requirement so that i can give proper solution.

0

Thanks for your help.

What i want to do is to generate timeline for data i have in mysql databases. I want to have only 1 generic timeline.html that will work on different JSON data files with diffrent time range.

Ok, i have a getData.php. When this file is called, it will query a parent database that contains names of other databases then print a list of database names that are clickable on screen. I want that when a user clicks on one of these names (let says database1), he will invoke function writeData() which queries database1 to get data, then writes data to a javascript file (such as parameter.js) when done, it calls timeline.html. Timeline.html uses parameter.js as data input to generate timeline.
The problem is that function writeData() will not work after getData.php finished.

I can use
echo "<a href=getData.php?action=callwriteData&var=database1Name">"
and if (isset($_GET(action)){writeData(); include("timeline.html")} it will work but it will call getData and print the same stuff on screen again.

Is there any other way to call writeData() without executing other functions in getData.php? or How to call writeData() using Ajax?
Thanks

Edited by 3tplus: correct

0

what about having a separate include file with the writeData() function in it?

functions.php

<?php
function writeData(){
//.........
}
?>

getData.php

<?
require_once 'functions.php';

if($this){
writeData();
}else{
writeData();
include('timeline.html');
}

Your situation does sound like a very good example where you would use Ajax though, heres an example of how to use ajax:

javascript file or inline script:

    <script type='text/javascript'>
    function getXMLHTTPRequest() {
       var req =  false;
       try {
          /* for Firefox */
          req = new XMLHttpRequest(); 
       } catch (err) {
          try {
             /* for some versions of IE */
             req = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (err) {
             try {
                /* for some other versions of IE */
                req = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (err) {
                req = false;
             }
         }
       }

       return req;
    }

function getData(action,query){
    var updReq = getXMLHTTPRequest();
    var vars = 'action='+action+'&query='+query;
    //alert(vars);
    var url = 'ajaxGetData.php';
    updReq.open('POST', url, true);
    updReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    updReq.onreadystatechange = function() {//Call a function when the state changes.
        if(updReq.readyState == 4 && updReq.status == 200) {
            alert(updReq.responseText);
        }
    }
    updReq.send(vars);
}
</script>

Then update the a tag like this:

<a href=getData.php?action=callwriteData&var=database1Name"></a>

<a href='javascript:' onclick="getData('callwriteData','database1Name');"></a>

You'll just need to change the getData.php file to echo out the data you want and rename it or update this line var url = 'ajaxGetData.php'; then it becomes available here alert(updReq.responseText); then tell the javascript what to do with the returned data

Edited by Biiim: script tag being weird & missed bit

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.