Call javascript function with php code

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 this. Everything else works except 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> 
+4
source share
4 answers

$myValue doesn't matter when trying to use it in JS.

PHP runs on the server, outputs an HTML document with embedded JavaScript, the document is sent to the client, and then JavaScript is executed.

If you do not know what value a JavaScript variable should have until PHP reaches the end of the document, you will not be able to generate this part of JS until then. You probably want to write it as an argument instead of calling a function.

Once you do this, you will have another problem - if your data is a string, then you need to specify it (and any corresponding quotation marks inside it must be escaped).

In a nutshell: PHP displays text that can 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 seems to be no reason to use JavaScript in the first place, and you would be better off moving all the logic to PHP.

By the way, your choice of Doctype will trigger Quirks mode in most browsers. This is almost always very undesirable.

The best choice:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

Or if you really want Transitional:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
+5
source

Modify javascript function to accept parameter (value $myValue )

 function doDecryption(param) 

Then change the value ct = hex2s(<?php echo $myValue; ?>); on ct = hex2s(param);

Finally, you need to pass the php $myValue variable to the $myValue function function, and you can do this when you call it

 echo '<script type="text/javascript"> doDecryption( '.$myValue;.'); </script>'; 

if the variable $myValue is a nuisance, then you should add quotes to it when submitting to javascript

 echo '<script type="text/javascript"> doDecryption( "'.$myValue;.'"); </script>'; 
+2
source

Can you clarify the question? If the question arises: can you call the client-side javascript function at runtime on your server-side PHP server? the answer is no.

If you started the nodeJS server, it can host the javascript server and make function calls if the javascript code is somewhere that nodejs can execute it.

In general, the use case is confusing to me. I assume that the application that was originally intended to work with decryption on the client side in the browser does this because the server does not have the keys to perform decryption. Therefore, if this is an application that someone wrote, and you are trying to make changes, you will want to make sure that you actually have the keys that you need on the server side to complete the decryption process, since it probably was never intended for a server to access decryption keys. Also, if the server really has access to the keys to perform decryption, perhaps this is a good way to do this with php code. (I am not php dev, so I can’t say exactly what PHP code you will need to write the php script decryption).

Hope this helps. Mark

0
source

Looks good if you are not using AJAX (if you might need eval () your answer)

I tried a short version of your code and it works 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

 Inside Javascriptwhats happening 

Hth, Regards.

-1
source

All Articles