Calling Javascript function within php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gsashwin
Forum Newbie
Posts: 4
Joined: Thu Nov 18, 2010 12:11 am

Calling Javascript function within php

Post by gsashwin »

Hi guys,

I am having a tough time executing the code inside the javascript. Everything is working fine with the Php code and I am able to see the output of variable myValue too.. But the problem is nothing is being executed inside the javascript..Not even the first statement of the javascript..When I look at the output sent by the HTML to the browser, the output is what is listed after this code which is right at the bottom of this page. Please advise.

Code: Select all

<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.println("Inside Javascript");
                var ct, key;
                var theForm = document.forms[0];
                blockSizeInBits=128;
                keySizeInBits = theForm.keySize[theForm.keySize.selectedIndex].value;

                if (theForm.key.value.toLowerCase().indexOf("0x") == 0)
                theForm.key.value = theForm.key.value.substring(2);
                if (theForm.ciphertext.value.toLowerCase().indexOf("0x") == 0)
                theForm.ciphertext.value = theForm.ciphertext.value.substring(2);

                if (theForm.key.value.length*4 != keySizeInBits)
{
                alert("For a " + keySizeInBits + " bit key, the hex string needs to be " +
                (keySizeInBits / 4) + " hex characters long.");
                if (theForm.key.select)
                theForm.key.select();
                return;
 }

      if (theForm.ciphertext.value.length*4 != blockSizeInBits)
{
        alert("For a " + blockSizeInBits + " bit block, the hex ciphertext string needs to be " +
              (blockSizeInBits / 4) + " hex characters long.");
        if (theForm.ciphertext.select)
           theForm.ciphertext.select();
        return;
      }

      ct = hex2s(<?php echo $myValue; ?>);
      document.println("Inside Javascript");
      document.write(ct);
     // key = hex2s(theForm.key.value);
     // theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB"));


                }


</script>
</head>
[syntax=php]
<?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";
                    //doDecryption();

            }
            echo '<script type="text/javascript">
    doDecryption();
    </script>';
    echo "whats happening";
?>
</body>
</html>
[/syntax]
<body>




CODE THAT IS SENT AS HTML OUTPUT TO THE BROWSWER

Code: Select all


<!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" language="JavaScript"> 
 
   function doDecryption()
                {
                document.alert("Inside Javascript");
                var ct, key;
                var theForm = document.forms[0];
                blockSizeInBits=128;
                keySizeInBits = theForm.keySize[theForm.keySize.selectedIndex].value;
 
                if (theForm.key.value.toLowerCase().indexOf("0x") == 0)
                theForm.key.value = theForm.key.value.substring(2);
                if (theForm.ciphertext.value.toLowerCase().indexOf("0x") == 0)
                theForm.ciphertext.value = theForm.ciphertext.value.substring(2);
 
                if (theForm.key.value.length*4 != keySizeInBits)
{
                alert("For a " + keySizeInBits + " bit key, the hex string needs to be " +
                (keySizeInBits / 4) + " hex characters long.");
                if (theForm.key.select)
                theForm.key.select();
                return;
 }
 
      if (theForm.ciphertext.value.length*4 != blockSizeInBits)
{
        alert("For a " + blockSizeInBits + " bit block, the hex ciphertext string needs to be " +
              (blockSizeInBits / 4) + " hex characters long.");
        if (theForm.ciphertext.select)
           theForm.ciphertext.select();
        return;
      }
 
      ct = hex2s(<br />
<b>Notice</b>:  Undefined variable: myValue in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>41</b><br />
);
      document.write("Inside Javascript");
      document.write(ct);
     // key = hex2s(theForm.key.value);
     // theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB"));
 
 
                }
 
 
</script> 
</head> 
 
<body> 
<br /> 
<b>Notice</b>:  Undefined index: userId in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>58</b><br />  // Do not worry about this notice because teh value has not been posted it is not able find this value.  
</body>
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: Calling Javascript function within php

Post by Zyxist »

The problem is self-explainatory:

Code: Select all

<b>Notice</b>:  Undefined variable: myValue in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>41</b>
It tells you everything: where it occurs and what kind of thing you must fix. You haven't defined a PHP variable and you try to use it.
Post Reply