can anyone tech me how yo pass values from php script to javscript??
8 36126
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]
we usually put the echo or print line in the value of a "hidden" text box. -
-
<input type="hidden" name="someVar" id="someVar" value="<?php echo $myVar; ?>" />
-
-
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: -
alert("Value is " + document.getElementById("someVar").value);
-
-
If you're printing this value elsewhere on the page, put inside a tag and give that tag an id for example: -
-
<div id="container">
-
<span id="someVal"><?php echo $myVal; ?></span>
-
</div>
-
-
and you can access that with the attribute "innerHTML" such as -
-
alert("Value is " + document.getElementById("someVar").innerHTML);
-
-
if you STILL don't get it, Google: "miracle".
Dan
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: -
<html>
-
<head>
-
<title>Test stuff</title>
-
<script type="text/javascript">
-
// Create a var in the global scope.
-
// Can be accessed by any Javascript that follows, anywhere in the page.
-
var phpValue = "<?php echo $someValue; ?>";
-
</script>
-
</head>
-
-
<!-- This should print the value when the page loads -->
-
<body onload="javascript: alert(phpValue);">
-
<h1>Nothing to see here... move along</h1>
-
</body>
-
</html>
-
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.
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?>)";
-
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.
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.
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?>
- <?php $xphp= "My PHP WORLD"; ?> <!-- Define a PHP variable -->
-
<script> var xjs='My Java Script world'; </script> <!-- Define a JAVASCRIPT variable -->
-
<input type='hidden' id='myhtml' value='My HTML world!' > <!-- Define a HTML variable -->
-
-
<BR>sending PHP variable value into JAVASCRIPT <BR>
-
<script>
-
var xphp='<?php echo $xphp; ?>';
-
document.write(xphp);
-
</script>
-
-
<BR>getting PHP variable value into HTML <BR>
-
<?php echo $xphp; ?>
-
-
<BR><BR>getting JAVASCRIPT variable value into PHP <BR>
-
<?php
-
$xjs = "<script>document.write(xjs);</script>";
-
echo $xjs;
-
?>
-
-
<BR>getting JAVASCRIPT variable value into HTML <BR>
-
<script>document.write(xjs);</script>
-
-
<BR><BR>getting HTML variable value into JAVASCRIPT <BR>
-
<script>
-
var xhtml=document.getElementById('myhtml').value;
-
document.write(xhtml);
-
</script>
-
-
<BR>getting HTML variable value into PHP <BR>
-
<?php
-
$xhtml = "<script>document.write(document.getElementById('myhtml').value);</script>";
-
echo $xhtml;
-
?>
Sign in to post your reply or Sign up for a free account.
Similar topics
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)
{...
|
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:
-----------------------------------------------------------------------...
|
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...
|
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) {...
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
| |