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
https://forums.phpfreaks.com/topic/220460-calling-javascript-function-within-php/
Share on other sites

Well you are echoing values of a variable that has no value, so you're getting NOTICE warnings.  You can turn down the errorlevel so that notices are not shown, but that won't fix problems you'll still encounter.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.