简体   繁体   English

将PHP变量传递给Javascript函数(特定代码错误)

[英]Passing PHP Variable to Javascript Function (Specific code error)

So I've searched around a ton and figured out how to pass a PHP variable to a Javascript function, but when I impliment it in my code, rather than getting the variable in the alert(); 因此,我已经搜索了很多,并弄清楚了如何将PHP变量传递给Javascript函数,但是当我将其隐含在代码中时,而不是在alert()中获取变量; window I get <?php echo $a ?> and <?php echo $c ?> Hopefully it's something small that I'm overlooking, but I have no idea why this isn't functioning as I copied it line for line from a response on a forum and the user stated that it works, and I had a friend who stated that it worked, but when I ran the code on it's own (Second [code][/code]) it returns the same error. 窗口我得到<?php echo $a ?><?php echo $c ?>希望这是我忽略的小东西,但是我不知道为什么这不起作用,因为我从a逐行复制了它在论坛上的回复,用户说它可以工作,我有一个朋友说它可以工作,但是当我自己运行代码(第二个[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>

Stand alone code that results in the same display 产生相同显示的独立代码

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

Thank you guys!!! 感谢大伙们!!!

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.). 就语法而言,您的代码是正确的,但是,正如注释中所指出的那样,您始终需要为输出到的介质(数据库,其他编程语言,html等)准备变量。

The problem is that your php does not get parsed. 问题是您的php无法解析。

The reasons could be: 原因可能是:

  • You do not have php enabled on your server; 您没有在服务器上启用php;
  • Your filename ends for example in .html instead of .php causing the file not to be recognized as a php file; 例如,您的文件名以.html而不是.php导致该文件不被识别为php文件;
  • You are not requesting the file from the server but from the file-system. 您不是从服务器请求文件,而是从文件系统请求文件。

/n is not new line. / n不是换行符。 \\n is. \\ n是。

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

tested and works fine. 经过测试,工作正常。

Also, are you saving the file as .php type and not .html? 另外,您是否将文件另存为.php类型而不是.html? As you may have surmised, PHP will not work in .html files. 如您所料,PHP无法在.html文件中工作。 You will need to save as .php and also, as jeroen said, make sure php is enabled. 您将需要另存为.php,并且,如jeroen所说,请确保已启用php。

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.   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM