admin管理员组文章数量:1244215
I am trying to call the Javascript function declared at the top in my php area. However its not working. Can anyone tell me the reason for it. Everything else is working except for this part. Please help me.
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AES (Rijndael) Encryption Test in JavaScript</title>
<script src="aes-enc.js" type="text/javascript" language="JavaScript"></script>
<script src="aes-dec.js" type="text/javascript" language="JavaScript"></script>
<script src="aes-test.js" type="text/javascript" language="JavaScript"></script>
<script type="text/javascript">
function doDecryption()
{
document.write("Inside Javascript");
var ct, key;
ct = hex2s(<?php echo $myValue; ?>);
document.write("Inside Javascript");
document.write(ct);
// key = hex2s(theForm.key.value);
// theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB"));
}
</script>
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("encryption") or die(mysql_error());
$userId = $_POST['userId'];
if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'] == ""))
{
$query = mysql_query("select * from employee_details where id = '$userId'"); if($row=mysql_fetch_assoc($query))
{
echo '<tr>';
foreach($row as $value)
echo '<td>'.$value.'</td>';
echo '</tr>';
}
else { echo "No rows returned"; }}
else if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key']))
{
$columname = "ciphertext";
$tablename = "employee_details";
function getField($field, $tbl_name, $condition)
{
$result = mysql_query("SELECT $field FROM $tbl_name WHERE id = ".$condition);
return @mysql_result($result, 0);
}
$myValue = getField($columname,$tablename,$userId);
echo "$myValue";
[B]echo '<script type="text/javascript">
doDecryption();
</script>';[/B]
echo "whats happening";
//doDecryption();
}
?>
</body>
</html>
I am trying to call the Javascript function declared at the top in my php area. However its not working. Can anyone tell me the reason for it. Everything else is working except for this part. Please help me.
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AES (Rijndael) Encryption Test in JavaScript</title>
<script src="aes-enc.js" type="text/javascript" language="JavaScript"></script>
<script src="aes-dec.js" type="text/javascript" language="JavaScript"></script>
<script src="aes-test.js" type="text/javascript" language="JavaScript"></script>
<script type="text/javascript">
function doDecryption()
{
document.write("Inside Javascript");
var ct, key;
ct = hex2s(<?php echo $myValue; ?>);
document.write("Inside Javascript");
document.write(ct);
// key = hex2s(theForm.key.value);
// theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB"));
}
</script>
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("encryption") or die(mysql_error());
$userId = $_POST['userId'];
if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'] == ""))
{
$query = mysql_query("select * from employee_details where id = '$userId'"); if($row=mysql_fetch_assoc($query))
{
echo '<tr>';
foreach($row as $value)
echo '<td>'.$value.'</td>';
echo '</tr>';
}
else { echo "No rows returned"; }}
else if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key']))
{
$columname = "ciphertext";
$tablename = "employee_details";
function getField($field, $tbl_name, $condition)
{
$result = mysql_query("SELECT $field FROM $tbl_name WHERE id = ".$condition);
return @mysql_result($result, 0);
}
$myValue = getField($columname,$tablename,$userId);
echo "$myValue";
[B]echo '<script type="text/javascript">
doDecryption();
</script>';[/B]
echo "whats happening";
//doDecryption();
}
?>
</body>
</html>
Share
Improve this question
edited Dec 2, 2010 at 9:03
Lekensteyn
66.5k25 gold badges167 silver badges203 bronze badges
asked Dec 2, 2010 at 8:58
CentosUserCentosUser
2011 gold badge4 silver badges15 bronze badges
6
- What does the output look like? – jensgram Commented Dec 2, 2010 at 9:04
- I figured out why this is failing after running the code in my head. It would have been rather easier to solve if you had provided error messages. For that matter, as a rule of thumb, try to break your question down into either "Why doesn't this JS work?" or "Why doesn't this PHP output this HTML/JS source code?" instead of "Why doesn't the HTML/JS source code generated by PHP not do what I expect?" – Quentin Commented Dec 2, 2010 at 9:08
-
DANGER : You have an SQL Injection Security Vulnerability! Do not build SQL queries by sticking together strings that include unsanitized
$_POST
data! bobby-tables. – Quentin Commented Dec 2, 2010 at 9:11 - @David: Wow, I didn't know that they even made a site for Little Bobby Tables (can't access it, though ): ). – Marcel Korpel Commented Dec 2, 2010 at 10:31
- There are several, that's one of the better ones. Unfortunately, it does seem to have fallen over since I made my previous ment. No doubt it will back back up before long. It explains the issues and how to avoid them in a number of different languages (generally by explaining the various methods of using parametrized queries) – Quentin Commented Dec 2, 2010 at 10:45
4 Answers
Reset to default 5$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 HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3/TR/html4/strict.dtd">
Or if you really want Transitional:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3/TR/html4/loose.dtd">
Change you javascript function to take a parameter (the value of $myValue
)
function doDecryption(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 '<script type="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 '<script type="text/javascript">
doDecryption( "'.$myValue;.'");
</script>';
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
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 HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AES (Rijndael) Encryption Test in JavaScript</title>
<script src="aes-enc.js" type="text/javascript" language="JavaScript"></script>
<script src="aes-dec.js" type="text/javascript" language="JavaScript"></script>
<script src="aes-test.js" type="text/javascript" language="JavaScript"></script>
<script type="text/javascript">
function doDecryption()
{
document.write("Inside Javascript");
}
</script>
</head>
<body>
<?php
echo '<script type="text/javascript">
doDecryption();
</script>';
echo "whats happening";
?>
</body>
</html>
And the result is
Inside Javascriptwhats happening
HTH, Regards.
本文标签: Calling javascript function with php codeStack Overflow
版权声明:本文标题:Calling javascript function with php code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740214519a2242618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论