473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

php & javascript interaction

25
Hi,

can PHP be used to execute a JavaScript function?
And, can JavaScript be used to execute a PHP function?

if so, what is the basic coding to perform these actions.

thank you
Adrian
Sep 6 '06 #1
6 6558
phpmaet
27
Yes, Let's execute Javascript function in PHP. you can see the following example.

<title>Untitled Document</title>
<SCRIPT LANGUAGE="javascript">
function Test()
{
alert ("This is Sample");
}
</SCRIPT>
</head>


<body>
<?
echo "<SCRIPT LANGUAGE='javascript'>";
echo "Test();";
echo "</SCRIPT>";
?>


Thanks
Sep 6 '06 #2
Banfa
9,065 Expert Mod 8TB
I disagree with phpmaet

php can not execute Javascript, Javascript can not execute php. This is beacuse php runs at the server and javascript runs at the client.

What phpmaet has done is use php to write javascript which will then run at the client, this might be a slightly subtle distinction but I beleiev it is an important 1.

I have used this trick myself (having php write javascript) and it is completely legal but php is not running javascript it is writing it for future exection. The javascript, as always, runs at the client computer.
Sep 6 '06 #3
phpmaet
27
i also disagree with Banfa

PHP CAN execute javascript.

Javascript CAN execute php


you should cut & paste and run my script.


sample.php
----------

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<SCRIPT LANGUAGE="javascript">
document.write("<? echo 'This is my inside script<BR><BR>'; ?>");
function Test()
{
alert ("This is Sample");
}
function fnClick()
{
window.location.href='sample.php?c=1';
}
</SCRIPT>
</head>
<body>
<?
if($_GET['c'])
{
echo "<SCRIPT LANGUAGE='javascript'>";
echo "Test();";
echo "</SCRIPT>";
}
?>
<input type="button" name="txs" onclick="fnClick()" value="Click here" />
</body>
</html>
Sep 6 '06 #4
ronverdonk
4,258 Expert 4TB
Banfa, you are absolutely right!
What is often forgotten is that PHP can only send HTML (and JS) to the client where it will execute, but not under control of PHP. PHP is merely the echo-er here.

Ronald :cool:
Sep 6 '06 #5
adriann
25
thanks guys. i can now understand the subtle process that are happening here.
thanks also for the code examples.
Sep 6 '06 #6
Same Page Data Interaction

Expand|Select|Wrap|Line Numbers
  1. <?php  $xphp= "My PHP WORLD"; ?>   <!-- Define a PHP variable -->
  2. <script> var xjs='My Java Script world'; </script> <!-- Define a JAVASCRIPT variable -->
  3. <input type='hidden' id='myhtml' value='My HTML world!' > <!-- Define a HTML variable -->
  4.  
  5. <BR>sending PHP variable value into JAVASCRIPT <BR>
  6. <script>
  7. var xphp='<?php echo $xphp; ?>';
  8. document.write(xphp);    
  9. </script>
  10.  
  11. <BR>getting PHP variable value into HTML <BR>
  12. <?php echo $xphp; ?>
  13.  
  14. <BR><BR>getting JAVASCRIPT  variable value into PHP <BR>
  15. <?php
  16. $xjs = "<script>document.write(xjs);</script>";
  17. echo $xjs;
  18. ?>
  19.  
  20. <BR>getting JAVASCRIPT  variable value into HTML <BR>
  21. <script>document.write(xjs);</script>
  22.  
  23. <BR><BR>getting HTML variable value into JAVASCRIPT  <BR> 
  24. <script>
  25. var xhtml=document.getElementById('myhtml').value;
  26. document.write(xhtml);    
  27. </script>
  28.  
  29. <BR>getting HTML variable value into PHP <BR> 
  30. <?php
  31. $xhtml = "<script>document.write(document.getElementById('myhtml').value);</script>";
  32. echo $xhtml;
  33. ?>
Mar 27 '17 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Shock | last post by:
Hi everybody, I just wrote my first two triggers and from the minimal amount of testing I have done, they work! However, I was hoping I could get some feedback from those of you more...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: KKramsch | last post by:
OK, here's the scenario: I have a CGI script that generates a page with frames (BTW, I'm not crazy about frames, but in this case the decision to use them is out of my hands). In a typical...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
1
by: Artur Kowalski | last post by:
I have a NotifyIcon in my Windows Service project and I am trying to add a ContextMenu to this NotifyIcon or use some of the mouse events. Everything isn't working. I think so base class of the...
0
by: David Virgil Hobbs | last post by:
While studying the asp.net javascript problem I saw and used pages about asp.net basics at http://www.sitepoint.com/article/815/4 and...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
0
by: Vijay | last post by:
Prep Courses for International Certifications, CSTE & CSQA & ISEB & ISTQB &Business Analyst & SOA Certifications in HYDERABAD. After receiving overwhelming response to our last 50+ batches, ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.