...

View Full Version : How to pass php value to javascript?



autumn
01-16-2012, 10:40 AM
Here is my code


<script language="javascript" type="text/javascript">
function revealModal(divID)
{

window.onscroll = function () { document.getElementById(divID).style.top = document.body.scrollTop; };
document.getElementById(divID).style.display = "block";
document.getElementById(divID).style.top = document.body.scrollTop;

}

function hideModal(divID)
{
document.getElementById(divID).style.display = "none";
}
</script>


<div id="modalPage3">
<div class="modalBackground"></div>
<div class="modalContainer">
<div class="modal53">
<div class="modalTop"><a href="javascript:hideModal('modalPage3')">[X]</a></div>
<div class="modalBody">
<?php echo 'value='.$_SERVER['value'];


?>
<h3><center>Choose from the options below</center></h3>
<center><div id="stylized" class="myform">

<a href="javascript:hideModal('modalPage3');javascript:revealModal('modalPage1')" tabindex="2" title="Remove UPCAT Passer"><img src="images/photo_remove.png" /></a>&nbsp;&nbsp;
<a href="javascript:hideModal('modalPage3');javascript:revealModal('modalPage2')" tabindex="4" title="Edit UPCAT Passer"><img src="images/photo_edit.png" /></a>&nbsp;&nbsp;
<a href="javascript:hideModal('modalPage3');javascript:revealModal('modalPage4')" tabindex="4" title="Change Status of UPCAT Passer"><img src="images/photo_up.png" /></a>
</div></center>
</div>
</div>
</div>
</div>


<a href=javascript:revealModal('modalPage3');>
<img src=images/option.jpg /></a>

my problem is this one..how can I pass a value example value=3 to be like
href= index.php?value=3..PLEASE HELP!!!

sunfighter
01-16-2012, 06:13 PM
php to js is a simple thing, just echo in the js.
Part one php and js only:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>New document</title>
<?php
$var_we_pass = '3';
?>

<script type="text/javascript">

href= 'index.php?value=' + <?php echo $var_we_pass; ?>;
alert(href);
</script>

Part two add php to the HTML:

</head>

<body>

<a href="javascript:hideModal('modalPage3');javascript:revealModal('modalPage4')" tabindex="<?php echo $var_we_pass; ?>" title="Change Status of UPCAT Passer">
<img src="images/photo_up.png" /></a>

</body>
</html>

jalarie
01-16-2012, 09:38 PM
Move your php-echo line up a few lines so that it is within the script:


<?php echo 'value='.$_SERVER['value'];?>

felgall
01-17-2012, 09:20 AM
If you use

<script type="text/javascript" src="scriptname.php">

to attach the JavaScript to the page then the file can contain PHP that generates the JavaScript code dynamically.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum