Jump to content

Get a variable value from a javascript function


grs5211

Recommended Posts

I have a function that gets the person who last modified a document. In simple terms here is the function.

function extractModifiedBy()

{

var ModCode = 'It was John Doe that was here' ;

 

//alert("Extract code=" + ModCode.substr(8,8));

return ModCode.substr(8,8);

    }

I wish to diaplay this value in a table row.

 

When I run this code I get no result.

<td colspan="1">

    <?php echo "Last Modified by : " . "<SCRIPT LANGUAGE='javascript'>extractModifiedBy();</SCRIPT>" ?>

</td>

Does it alert correctly?? try calling the function from a button thats not brought it by PHP and see if the js is working.

 

other than that all i can see is invaild markup

 

don't mix your lower case and up case tag names always stick to lower case and escape your " rather then use ' 

 

example

<?php echo "Last Modified by : " . "<script language=\"javascript\">extractModifiedBy();</script>" ?>

//also try

"<script type=\"text/javascript\"> // seems to work better for me!

You can directly pass variables between Javascript and PHP.  Javascript is client side and PHP is server side.

 

There are two solutions to work around the problem.

 

1.  Use Javascript to redirect the page and add the variable to the url (http://example.com?passed=variables)

2.  Use Javascript to change the content of the td

Exactly. J.Daniels was only have right. You can pass php variables to javascript, but you cant pass javascript variables to php. And in this case, the problem wasn't related to that anyways, in that this really wasnt a php issue at all. The problem was that your function returned a value, but didn't do anything with it - i.e. print it to the page. gijew gave you a solution that will work (not one I would use, but it will still work).

Thanks for all the responses. I know this method is a bit unorthodox, but it would really solve the issue I have in this contect.

Well..tried all the above, but the function was never fired. I have an alert in the function, but I don't see it.

Testing the function with a button works just fine.

Is there any reason why <?php echo "Last Modified by : " . "<script language=\"javascript\">extractmodifiedby();</script>"; ?> will not fire the function.

This code is placed directly after the <form> tag.

 

</html>
<head>
<script language=javascript type='text/javascript'>
function extractModifiedBy() {
var ModCode = 'It was John Doe that was here' ;

//alert("Extract code=" + ModCode.substr(8,);
return ModCode.substr(8,;
}

function setMod() {
var lastMod = document.getElementById("lastMod");
var modBy = extractModifiedBy();
lastMod.innerHTML = modBy;
}
</script>

</head>
<body onload="setMod()">

Last Modified by : <div id="lastMod"></div>

</body>
</html>
[code]

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.