Calling Javascript Function With Php Code
Solution 1:
$myValue
doesn't have a value when you try to use it in the JS.
The PHP runs on the server, outputs an HTML document with embedded JavaScript, the document is sent to the client and then the JavaScript runs.
If you don't know what value the JavaScript variable needs to have until the PHP gets to the end of the document, then you can't generate that part of the JS until then. You probably want to write it as an argument to the function call instead.
Once you do that you have another problem — if your data is a string, then it needs to be quoted (and any matching quote marks inside it need to be escaped).
In a nutshell: PHP outputs text that might be processed as JS, it cannot call JavaScript functions (unless you start mixing extensions that can talk to Rhino/Spidermonkey/etc on the server).
All that said, in this case, there doesn't seem to be any reason to use JavaScript in the first place and you would be better off moving all the logic to PHP.
Incidentally, your choice of Doctype will trigger Quirks mode in most browsers. This is almost always highly undesirable.
A better choice would be:
<!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
Or if you really want Transitional:
<!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
Solution 2:
Change you javascript function to take a parameter (the value of $myValue
)
functiondoDecryption(param)
Then change the ct = hex2s(<?php echo $myValue; ?>);
to ct = hex2s(param);
Last, you need to pass the $myValue
php variable to the javsacript function function and you can do this when you call it
echo '<scripttype="text/javascript">doDecryption( '.$myValue;.');
</script>';
if you $myValue
variable is a stirng, then you should add quotes around it when feeding it to javascript
echo '<scripttype="text/javascript">doDecryption( "'.$myValue;.'");
</script>';
Solution 3:
Can you clarify the question? If the question is: can you call a client side javascript function, at runtime in your server side PHP code? the answer is no.
if you ran a nodeJS server it's possible to host the javascript server side and execute the function calls as long as the javascript code resides somewhere that nodejs can execute it.
Overall though, the use case is confusing to me. I am guessing that an application that was originally designed to do decryption client side in the browser does so because the server does not have the keys to perform the decryption. So if this was an application that someone else wrote and you are trying to make changes to, you will want to make sure that you actually have the keys you need server side to perform the decryption process, since it was probably never intended for the server to have access to the decryption keys. Additionally, if the server does have access to the keys to perform the decryption, there is probably a nice way to do this with php code. (I'm not a php dev, so I can't speak to exactly what php code you would need to write the decryption php script).
Hope this helps. Mark
Solution 4:
It looks well if you are not using AJAX (if you are may be you need to eval() your response)
I have tried a shorted version of your code and it is working fine.
<!doctype HTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>AES (Rijndael) Encryption Test in JavaScript</title><scriptsrc="aes-enc.js"type="text/javascript"language="JavaScript"></script><scriptsrc="aes-dec.js"type="text/javascript"language="JavaScript"></script><scriptsrc="aes-test.js"type="text/javascript"language="JavaScript"></script><scripttype="text/javascript">functiondoDecryption()
{
document.write("Inside Javascript");
}
</script></head><body><?phpecho'<script type="text/javascript">
doDecryption();
</script>';
echo"whats happening";
?></body></html>
And the result is
Inside Javascriptwhats happening
HTH, Regards.
Post a Comment for "Calling Javascript Function With Php Code"