CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2005
    Posts
    63

    Talking JAVASCRIPT & PHP

    hi,
    how can i call a javascript function from php code? Has nayone an example?

    Thanks

  2. #2
    Join Date
    Dec 2004
    Posts
    438

    Re: JAVASCRIPT & PHP

    You can write Javascript using PHP, but PHP is on the server-side, while Javascript is on the client side, they can't interact like that as far as I'm aware.

    Javascript is used to alter things in the HTML output, produce new windows, do image rollovers etc. PHP takes the content sent from a html web page and then does something with it, such as produce another html page, interact with a database, create a file etc.
    Last edited by Nibinaear; January 23rd, 2007 at 07:30 AM.

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: JAVASCRIPT & PHP

    JavaScript and PHP can interact using AJAX or passing variables to the URL. That is all.

    By writing JavaScript, Nibinaear meant to echo it.
    PHP Code:
    <?php
    echo "
      <script language='JavaScript'>
        alert('Hi!');
      </script>
    "
    ;
    ?>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4

    Re: JAVASCRIPT & PHP

    PHP and Javascript are like good friends working together, but at different locations, rarely seeing each other. While Javascript works on the browser, satisfying the client local request, PHP is often called by Javascript when he needs something to be done by the server, like for example, retrieving and formating data from a database.

    They can call each other using form submission:
    Code:
     
    <SCRIPT Language="JavaScript">
    function TellJavaScripttoCallServerSideScript()
    {
    document.form.submit(); 
    }
    </SCRIPT>
     
    <form name="form" action="GiveMeSomeHTMLResult.php" method="post" target="_self">
    ...
     
    <INPUT TYPE="BUTTON" ONCLICK="TellJavaScripttoCallServerSideScript();"
    </form>
    Or by using Ajax which allows you to perform only localized specific task on your form:

    Code:
    <script Language="JavaScript">
    function ajaxFunction()
    { 
    var xmlHttp;
    try
    {	
    //Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();	
    }
    catch (e)
    {	
    	// Internet Explorer	
    	try
    	{	 
    	 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	 
    	}
    	catch (e)
    	{	 
    	 try
    	 {		
    	 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");		
    	 }
    	 catch (e)
    	 {		
    	 alert("Your browser does not support AJAX!");		
    		return false;		
    	 }
    	}
    } 
    xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
    	{
    	 document.getElementById("test").innerHTML=xmlHttp.responseText;
    	}
    }
    xmlHttp.open("GET","GiveMeSomeHTMLResult.php", true);	 
    xmlHttp.send(null);
    }
    </SCRIPT> 
     
    <FORM name="myForm">
    <DIV id="test" name="test">
    <SCRIPT Language="JavaScript">
    ajaxFunction();
    </SCRIPT> 
    </DIV>
    PHP can also generate its own Javascript code, like the PeejAvery sampled, simply by writting it, to be used locally, and this will be the only way i know for a PHP function to call a JavaScript one: only by rewritting it.

    What do you have in mind?
    David Domingues at webrickco@gmail.com. Feel free to visit http://www.webrickco.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured