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

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";

<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>

<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!!!

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">
<title>New document</title>
$var_we_pass = '3';

<script type="text/javascript">

href= 'index.php?value=' + <?php echo $var_we_pass; ?>;

Part two add php to the HTML:



<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>


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'];?>

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.