2

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(); 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.

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

3 Answers 3

3

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.
6
  • well php is enabled on the server as I have a form that runs to send email so I know that's not the issue. with the .html extension would that only create an issue if the server isn't running php? I know that you can run php within a .html page with no issue, and also I've run the file as a .php and the same issue occurs. How can I check if the file is being requested from the file system? I guess it would make sense that if the variables are defined locally that it may not store them server side... if that's the case would I have to create a separate .php file and call the file? Commented Nov 17, 2012 at 1:13
  • @PCort What is the extension of the filename? Commented Nov 17, 2012 at 1:14
  • the extension is .html The question then arises, can't you run php inline with html? Or would that just be in certain cases? Commented Nov 17, 2012 at 1:28
  • the extension is necessary for php to be interpreted. You can do inline php so long as the conditions in this answer are met. So "certain cases" means whenever you execute a php script. typically denoted by the extension .php - not to sound snide, but it seems that point has been decidedly clear in both answers here. Commented Nov 17, 2012 at 1:39
  • 2
    '<?php echo $a ?>' is not the right way to pass PHP variables into JS, unless you know it doesn't contain any special characters like quotes. In general, <?php echo json_encode($a); ?> without the outer quotes and possibly wrapped in htmlspecialchars in certain contexts, is more suitable. Commented Nov 17, 2012 at 7:55
1

/n is not new line. \n is.

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

9
  • The /n does not really matter as the string is in single quotes. Commented Nov 17, 2012 at 1:11
  • Sigh... I don't get it... Every time I try it something is FUBAR'd and I don't get the variable value.. I can't figure if it's something server side or client side, but I've run it on the godaddy server where the site is hosted, or off my WAMP server and I get the same issue, same with running it in Firefox, Chrome, and Opera.. I'm completely miffed why it isn't working for me. Commented Nov 17, 2012 at 1:12
  • @jeroen - well, just running his page on my own server, nl2br works as expected with \n but not /n. single or double quotes. I assumed such as well since all html emails we write use double quotes with new line markers, but it tests fine in this case. go figure Commented Nov 17, 2012 at 1:13
  • @PCort - just for clarity, what IS the filename? Commented Nov 17, 2012 at 1:14
  • @Kai Qing Just meant to say that it is not related to the problem, although you are right of course. Commented Nov 17, 2012 at 1:16
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.   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.