Jump to content

Calling Javascript function within php


gsashwin

Recommended Posts

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.

 

<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

 


<!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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.