473,397 Members | 1,960 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,397 software developers and data experts.

Passing values from PHP to JavaScript

darksteel21
can anyone tech me how yo pass values from php script to javscript??
Aug 5 '08 #1
8 36128
Gulzor
27
A simple output does the job :

[php]
<script type="text/javascript">
function hello(name) {
alert("hello "+name);
}

<?php
while ( $rec = mysql_fetch_assoc($query) ) {
echo 'hello("',$rec['firstname'],'");', "\n";
}
?>

// or

hello("<?php echo $var; ?>");

</script>
[/php]
Aug 5 '08 #2
dlite922
1,584 Expert 1GB
we usually put the echo or print line in the value of a "hidden" text box.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <input type="hidden" name="someVar" id="someVar" value="<?php echo $myVar; ?>" />
  3.  
  4.  
and then you can reach it via javascript once the page has been written (remember javascript = runs on client computer, PHP executes on the server before its delivered to the client computer)

So the browser sees the page as

[HTML]

<input type="hidden" name="someVar" id="someVar" value="YOUR_MY_VAR_VALUE_HERE" />

[/HTML]

and now JavaScript can access it this way:

Expand|Select|Wrap|Line Numbers
  1. alert("Value is " + document.getElementById("someVar").value);
  2.  
  3.  

If you're printing this value elsewhere on the page, put inside a tag and give that tag an id for example:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <div id="container">
  3.    <span id="someVal"><?php echo $myVal; ?></span>
  4. </div>
  5.  
  6.  
and you can access that with the attribute "innerHTML" such as

Expand|Select|Wrap|Line Numbers
  1.  
  2. alert("Value is " + document.getElementById("someVar").innerHTML);
  3.  
  4.  
if you STILL don't get it, Google: "miracle".


Dan
Aug 5 '08 #3
Atli
5,058 Expert 4TB
we usually put the echo or print line in the value of a "hidden" text box.
...
Just a thought.
Do you do this just to pass the values into JavaScript?

If so, you could simply add it to the header as a global variable.
Like:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.   <title>Test stuff</title>
  4.   <script type="text/javascript">
  5.     // Create a var in the global scope.
  6.     // Can be accessed by any Javascript that follows, anywhere in the page.
  7.     var phpValue = "<?php echo $someValue; ?>";
  8.   </script>
  9. </head>
  10.  
  11. <!-- This should print the value when the page loads -->
  12. <body onload="javascript: alert(phpValue);">
  13.   <h1>Nothing to see here... move along</h1>
  14. </body>
  15. </html>
  16.  
Doesn't really make sense to me to add actual HTML markup for each variable, when you can simply add it to a Javascript block.
Aug 6 '08 #4
i agree with atli ....

if u want to pass php value to javascript as parameter then u can do it this way...

say u have a function in javascript by name passPhpValue(parm1);
Expand|Select|Wrap|Line Numbers
  1. passPhpValue(parm1)
  2. {
  3. alert("This is PHP value:"+parm1);
  4. }
  5.  
  6. javascript:onload="passPhpValue(<?=$phpvalue?>)";
  7.  
Aug 6 '08 #5
Gulzor
27
i agree with atli ....

if u want to pass php value to javascript as parameter then u can do it this way...

say u have a function in javascript by name passPhpValue(parm1);

passPhpValue(parm1)
{
alert("This is PHP value:"+parm1);
}

javascript:onload="passPhpValue(<?=$phpvalue?>)";
You should not use this <?=$var?> (on its way to deprecation), but this <?php echo $var; ?> instead.
Aug 6 '08 #6
Atli
5,058 Expert 4TB
You should not use this <?=$var?> (on its way to deprecation), but this <?php echo $var; ?> instead.
A good point, but the short-tags have not been made deprecated just yet.
They will still exist in PHP6, but like in PHP5, they will not be enabled by default.
To use them you must reconfigure PHP.

So, like you say, using the short-tags should be avoided at all costs. By using the standard tags <?php ... ?>, you ensure that your code will always work as expected, no matter how the server is configured.
Aug 7 '08 #7
A good point, but the short-tags have not been made deprecated just yet.
They will still exist in PHP6, but like in PHP5, they will not be enabled by default.
To use them you must reconfigure PHP.

So, like you say, using the short-tags should be avoided at all costs. By using the standard tags <?php ... ?>, you ensure that your code will always work as expected, no matter how the server is configured.


thanx Atli

for the information
from now on i will use <?php echo $id?> instead of <?=$id?>
Aug 7 '08 #8
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 #9

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

Similar topics

3
by: Raju V.K | last post by:
can I use javascript and PHP in the following manner to create a pop-up window: <--- in <head> </head> <script language=javascript> function popup(folder1, file1) {...
1
by: Consuelo Guenther | last post by:
Hello, I am having problems with passing variables between pages. I have the following: First asp page has the function: -----------------------------------------------------------------------...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: cirillo_curiosone | last post by:
Hi, i'm new to javascript. I started studing it on the web few weeks ago, but still haven't been able to solve one big problem: HOT TO PASS VALUES FROM A SCRIPT VARIABLE TO A CHILD HTML...
2
by: Gordon Hudson | last post by:
Hello Here is what I am trying to do: Make a hyperlink on a data entry form with its URL built from the values for fields in that entry on the database. So, I would go to record 65785 click...
3
by: Aaron | last post by:
I am having a little difficulty with a relatively simple task. I have a parent webform. I have a javascript attribute added to a button to open a new window when clicked. The user fills in a...
1
by: | last post by:
Hi, 1st, I did a search and could not find any info on this, the Google results were good, but I'm still have issues...So any help will be great. I have a frame page, which contains 3 frames...
2
by: Peter | last post by:
Hello I have the following tag <select name="cat" id="cat" onchange="popUpWin('cmPhaseReload.php?category='+document.getElementById('cat').options.value ,600,600,'yes')" class="tbl_result"...
7
by: vinodsk101 | last post by:
Hi all, I have one doubt, can we pass a variable from javascript to servlet??? In Brief, i am getting some values in javascript through jsp and the same values i want in servlet. Ya i know i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.