grs5211 Posted June 11, 2009 Share Posted June 11, 2009 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,); return ModCode.substr(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> Quote Link to comment Share on other sites More sharing options...
laPistola Posted June 11, 2009 Share Posted June 11, 2009 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! Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted June 12, 2009 Share Posted June 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
gijew Posted June 12, 2009 Share Posted June 12, 2009 Try changing: return ModCode.substr(8,; to document.write(ModCode.substr(8,; Quote Link to comment Share on other sites More sharing options...
haku Posted June 12, 2009 Share Posted June 12, 2009 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). Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted June 12, 2009 Share Posted June 12, 2009 Sorry.. I meant to say can't pass Quote Link to comment Share on other sites More sharing options...
haku Posted June 12, 2009 Share Posted June 12, 2009 Heh, and I meant 'only half right' Quote Link to comment Share on other sites More sharing options...
grs5211 Posted June 12, 2009 Author Share Posted June 12, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted June 12, 2009 Share Posted June 12, 2009 </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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.