无法将PHP变量传递给Javascript函数

时间:2010-12-03 08:05:42

标签: php javascript function

我正在尝试获取一个PHP变量来将值传递给示例javascript,但如果我尝试回显结果则不会显示任何内容。

我尝试将常规字符串作为参数传递,并且这些工作正常。如果我试图将PHP变量作为参数传递,它似乎没有用。

              JavaScript中的AES(Rijndael)加密测试

<script type="text/javascript" src="Decrypt.js"></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 "Ciphertext = $myValue";
                echo "<br>";
                //doDecryption();

        }
        echo '<script type="text/javascript">
doDecryption("<?php $myValue; ?>");
</script>';
echo "whats happening";
?>
</body>
</html>

JS文件

function doDecryption(param)
 {
    document.write(param);
    document.write("Programming change");
 }

提前致谢。任何帮助非常感谢!!!!

6 个答案:

答案 0 :(得分:3)

你不需要echo语句来构建javascript-part,只需试试这个:

<script type="text/javascript">
  doDecryption("<?php echo $myValue; ?>");
</script>

但是如果你不想改变它而你想通过echo写出-Block,你就不应该使用“

echo '<script type="text/javascript"> doDecryption(' . $myValue . '); </script>';

答案 1 :(得分:1)

我认为你应该尝试回应你的var:

doDecryption("<?php echo $myValue; ?>");

答案 2 :(得分:0)

我无法访问PHP atm来检查,但我认为它是因为你使用单引号而不会评估变量。使用双引号作为回声并转义“在字符串内部,看看会发生什么”

答案 3 :(得分:0)

是的,更好地“回应”你的变量。

答案 4 :(得分:0)

或将您的回声更改为此

echo '<script type="text/javascript">
doDecryption("'.$myValue.'");
</script>';

答案 5 :(得分:-1)

试试这个,

<script type="text/javascript" src="Decrypt.js"></script>
<?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 "Ciphertext = $myValue";
                echo "<br>";
                //doDecryption();

        } ?>
<script type="text/javascript">
doDecryption("<?php $myValue; ?>");
</script>

</head>

<body>
<?php
echo "whats happening";
?>
</body>
</html>