can anyone tech me how yo pass values from php script to javscript??
8 36133
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |