Hi,
How can i create a link in my PHP code (index.php) that when a user click on that link it will:
1. call doSomething() function within index.php
2. then call Myhtml.html

Thanks for your help

Recommended Answers

All 7 Replies

are you using iframes? do you want something like a loader?

<script type='text/javascript'>
    function doSomething(){
        window.location = 'http://www.google.co.uk/';
    }
</script>

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

Thank you very much. That's exactly what i need.

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

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.