Jump to content

[SOLVED] pass php value to javascript? Please help


sayedsohail

Recommended Posts

Here is my script, I am tryin to pass php value to javascript function.

 

<tr id="action" title="click to do something" onclick="passvar(<?php print '$pageNum';?>);">

?>


function passvar(pan)
{
var page = <?php echo($pageNum);?>;
var bage = pan;
alert("Your user ID is:" + page); 
alert("Your user ID is:" + bage); 
}

Here is my script, I am tryin to pass php value to javascript function.

 

<tr id="action" title="click to do something" onclick="passvar(<?php print '$pageNum';?>);">

 

Your problem is in the fact you are echoing your PHP variable within single quotes so it is literally printing out "$pageNum" instead of the value of the variable. Just change it to this, and you should be fine:

onclick="passvar(<?php print $pageNum;?>);"

 

@frost110 - Remember that PHP sending values to javascript can happen all day long, but you cannot communicate back to PHP from javascript without the use of HTTP requests (AJAX).

Here is the revised code, still it doesn't work.

 

 

<tr id="action" title="click to do something" onclick="passvar(<?php print $pageNum;?>);">

 

function passvar(pan)

{

var bage = pan;

alert("Your user ID is:" + num);

//onclick="document.location.href='foo.html'

}

 

<?php
$pageNum = 5;
?>
<tr id="action" title="click to do something" onclick="passvar(<?=$pageNum;?>);">

function passvar(pan)
{
var bage = pan;
alert("Your user ID is:" + num); 
//onclick="document.location.href='foo.html'
}

 

Make sure $pageNum has a value.

Here is the revised code, still it doesn't work.

 

Is that your entire script? In that example, you are trying to apply an onclick handler to a DOM object that is not even complete. Can you give us an example of what you're after, and we'll be more than happy to try to help you work through it.

 

Also, while frost110 has the gist of things, try to avoid the "<?=" opening PHP tag since it is deprecated.

You have a couple other serious issues with your javascript:

 

1) You need to try to declare javascript functions before you make a call to them

 

2) You need to reference the variable you have assigned (in your case, you are assigning the variable "bage" and then trying to alert with variable "num" that doesn't exist.

 

Here's a full sample page that's tested and works:

<?php
$myVar = 5;
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function passvar(pan) {
var bage = pan;
alert("Your user ID is: " + bage);
}
</script>
</head>

<body>
<div onclick="passvar('<?php echo $myVar; ?>')">My div</div>
</body>
</html>

thank you guys it works now.  actually its a very long script just for your info i am display two tables with pagination on one page and am trying to update the second table rows when a user click on any of the table1 row. thus a feel of master detail relationship data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.