Hi Guys,

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>

Recommended Answers

All 3 Replies

Hi there, Have a look at this: This is just an example of how you can inter-mingle php and js.
The function is actually PHP, but what's contained inside of it is JS

<?php
	Redirect("page.html");
?>
 
/**
 * JavaScript Redirect Function - Redirects the user should the result data be empty
 * @param string $url the dynamic url to redirect to
 */
function Redirect($url) 
{ ?>
	<script type="text/javascript">
		window.location = "<?php echo $url ?>"
	</script>
<?php 
}
<!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>
</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";
                    doDecryptionPHP();   //call the php function
            }

function doDecryptionPHP()
{ ?>
<script type="text/javascript">
    doDecryption();    

    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>
<?php
}            
?>
</body>
</html>

Perhaps something like that? I have no idea if that will work, but have a play around with it and I'm sure you can get it to work!

I dont want this page to be redirected. Want to use it in the same page..

That's why I said "it's just an example"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.