2

所以我搜索了很多,并想出了如何将 PHP 变量传递给 Javascript 函数,但是当我在我的代码中实现它时,而不是在 alert() 中获取变量;我得到了窗口<?php echo $a ?><?php echo $c ?> 希望这是我忽略的小东西,但我不知道为什么这不起作用,因为我从论坛上的响应中逐行复制它并且用户说它有效,我有一个朋友说它有效,但是当我自己运行代码时(第二个 [code][/code]),它返回相同的错误。

   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<?php 
$a = 'Fatber Christmas'; 
$b = '27 Sunshine Street /n America'; 
$c = nl2br($b); 
?>

<html>

<head>      
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Kenneth So Photography</title>
    <link rel="stylesheet" href="kennethsostylesheet.css" type="text/css">
    <link rel="shortcut icon" href="Photos/favicon.png" type="image/png">

    <script type="text/javascript"> 
    function test(a,b) { 
    alert(a); 
    alert(b); 
} 
    </script>

    <script type="text/javascript" src="picScript.js"></script>
</head>

<body>

    <a onClick="test('<?php echo $a ?>', '<?php echo $c ?>');">LINK</a> 



    <div id="headerBar">
        <div id="logo">
            <a href="javascript:void(0)" ><img src="Photos/FinalizedLogo.png" alt="logo" height="100px" width="400px" onclick="setNum(1)"></a> 
        </div>
        <div id="titleBar">
            <p>
            <a href="javascript:void(0)" onclick="setNum(1)">Home</a> |
            <a href="javascript:void(0)" onclick="setNum(2)">Automotive</a> |
            <a href="javascript:void(0)" onclick="setNum(3)">Nightlife</a> |
            <a href="/about.html">About Me</a> |
            <a href="/contact.html">Contact</a>
            </p>
        </div>
    </div>
    <div id="imageBox">
        <div id="imageView">
        </div>

        <div id="imagePreview">
        </div>
    </div>
</body>
</html>

产生相同显示的独立代码

<?php 
$a = 'Fatber Christmas'; 
$b = '27 Sunshine Street /n America'; 
$c = nl2br($b); 
?> 
<html> 
<head> 
<script type="text/javascript"> 
function test(a,b) { 
    alert(a); 
    alert(b); 
} 
</script> 
</head> 
<body> 
<a onClick="test('<?php echo $a ?>', '<?php echo $c ?>');">LINK</a> 
</body> 
</html>

谢谢你们!!!

4

3 回答 3

4

Your code is correct as far as the syntax goes, however - as pointed out in the comments - you always need to prepare your variables for the medium you are outputting to (database, other programming language, html, etc.).

The problem is that your php does not get parsed.

The reasons could be:

  • You do not have php enabled on your server;
  • Your filename ends for example in .html instead of .php causing the file not to be recognized as a php file;
  • You are not requesting the file from the server but from the file-system.
于 2012-11-17T01:09:37.303 回答
1

/n 不是新行。\n 是。

<?php 
$a = 'Fatber Christmas'; 
$b = '27 Sunshine Street \n America'; 
$c = nl2br($b); 
?> 

经过测试并且工作正常。

Also, are you saving the file as .php type and not .html? As you may have surmised, PHP will not work in .html files. You will need to save as .php and also, as jeroen said, make sure php is enabled.

于 2012-11-17T01:08:36.410 回答
0

I tested the code. But it works fine for me.

 i)I think you do not have php enabled server. 
 ii) Check your server that accepts the javascript code and php code
 iii) May be plugins problem. So if you are using netbeans, see the php tag and javascript variable in different color. So u can easily recognize the mistakes.   
于 2012-11-17T07:45:37.180 回答