用php代码调用javascript函数 [英] Calling javascript function with php code

查看:67
本文介绍了用php代码调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用我的php区域顶部声明的Javascript函数。然而它不起作用。任何人都可以告诉我它的原因。除了这一部分,其他一切都在起作用。请帮我。

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>


推荐答案

$ myValue 没有值。

PHP在服务器上运行,输出带有嵌入式JavaScript的HTML文档,文档被发送到客户端,然后JavaScript运行。

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

如果你不知道JavaScript变量需要具有什么值,直到PHP结束为止文档,然后你不能生成JS的那部分。您可能希望将其作为函数调用的参数写入。

If you don't know what value the JavaScript variable needs to have until the PHP gets to the end of the document, then you can't generate that part of the JS until then. You probably want to write it as an argument to the function call instead.

一旦这样做,您还有另一个问题 - 如果您的数据是字符串,那么它需要引用(并且其中的任何匹配引号都需要转义)。

Once you do that you have another problem — if your data is a string, then it needs to be quoted (and any matching quote marks inside it need to be escaped).

简而言之: PHP输出可能作为JS处理的文本,它不能调用JavaScript函数(除非你开始混合可以说话的扩展名)到服务器上的Rhino / Spidermonkey / etc。)

In a nutshell: PHP outputs text that might be processed as JS, it cannot call JavaScript functions (unless you start mixing extensions that can talk to Rhino/Spidermonkey/etc on the server).

所有这些都说,在这种情况下,似乎没有任何理由首先使用JavaScript最好将所有逻辑移到PHP。

All that said, in this case, there doesn't seem to be any reason to use JavaScript in the first place and you would be better off moving all the logic to PHP.

顺便提一下,您选择的Doctype将在大多数浏览器中触发Quirks模式。这几乎总是非常不受欢迎的。

Incidentally, your choice of Doctype will trigger Quirks mode in most browsers. This is almost always highly undesirable.

更好的选择是:

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

这篇关于用php代码调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆