can anyone tech me how yo pass values from php script to javscript??
Passing values from PHP to JavaScript
Collapse
X
-
Tags: None
-
A simple output does the job :
[php]
<script type="text/javascript">
function hello(name) {
alert("hello "+name);
}
<?php
while ( $rec = mysql_fetch_ass oc($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.
Code:<input type="hidden" name="someVar" id="someVar" value="<?php echo $myVar; ?>" />
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:
Code: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:
Code:<div id="container"> <span id="someVal"><?php echo $myVal; ?></span> </div>
Code:alert("Value is " + document.getElementById("someVar").innerHTML);
DanComment
-
Originally posted by dlite922we usually put the echo or print line in the value of a "hidden" text box.
...
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:
[code=html]
<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="javascr ipt: alert(phpValue) ;">
<h1>Nothing to see here... move along</h1>
</body>
</html>
[/code]
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.Comment
-
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(pa rm1);
[code=html]
passPhpValue(pa rm1)
{
alert("This is PHP value:"+parm1);
}
javascript:onlo ad="passPhpValu e(<?=$phpvalue? >)";
[/code]Comment
-
Originally posted by samikhan83i 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(pa rm1);
passPhpValue(pa rm1)
{
alert("This is PHP value:"+parm1);
}
javascript:onlo ad="passPhpValu e(<?=$phpvalue? >)";Comment
-
Originally posted by GulzorYou should not use this <?=$var?> (on its way to deprecation), but this <?php echo $var; ?> instead.
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.Comment
-
Originally posted by AtliA 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?>Comment
-
Code:<?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; ?>
Comment
Comment