Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a PHP page with some JavaScript code also, but this JavaScript code below doesn't seem to work, or maybe I'm way off!

I am trying something like this:

  var areaOption=document.getElementById("<?php echo @$_POST['annonsera_name']?>");
  areaOption.selected=true;

Also I have tried this, but it only alerts a BLANK alert-box:

    alert (<?php echo $test;?>); // I have tried this with quotes, double-quotes, etc... no luck

Am I thinking completely wrong here?

UPDATE

Some PHP code:

    <?php 
        $test = "Hello World!";
    ?>
share|improve this question
1  
You're missing the quotes here: alert (<?php echo "H";?>); Should be alert ('<?php echo "H" ?>'); – Ivan Krechetov Jan 4 '10 at 9:38
It would help if you updated your question with the entire surrounding markup. – micahwittman Jan 4 '10 at 9:44
Can you paste the resulting JavaScript that gets to the browser? – Boldewyn Jan 4 '10 at 9:47
1  
Wow, I haven't seen such a useless example ever since starting at SO. What do you want to tell us with your update? – Boldewyn Jan 4 '10 at 9:48
1  
In most cases, you should stay away from using @. It's slow and can causes debugging problems down the road. – Justin Johnson Jan 4 '10 at 9:56
show 1 more comment

6 Answers

up vote 2 down vote accepted

In your second example, you are missing quotes around the string (so H is interpreted as a variable - which you didn't set).

Test this:

alert (<?php echo "'H'";?>);

OR

alert ('<?php echo "H";?>');
share|improve this answer
The second alerts exactly this to the alert-box: <?php echo "H";?>... I want the H to be alerted, or a variable preferrably... The first suggestion alerts only a blank alert-box! – Anonymous12345 Jan 4 '10 at 9:44
3  
what is the extension of your file? is it .js? if so, php will not work (by default) – jrharshath Jan 4 '10 at 9:46
@harshath.jr, pleace specify that! The extension is js, yes! – Anonymous12345 Jan 4 '10 at 9:51
PHP code will only be interpreted as such when the script file ends with an extension the web server expects to be PHP code: .php (other extensions are possible, but that's a different subject). – micahwittman Jan 4 '10 at 9:55
@Camran detailed answer posted – jrharshath Jan 4 '10 at 10:01

PHP runs on the server side and Javascript is running on the client side.

The process is that PHP generates the Javascript that will be executed on the client side.

You should be able to check the JS that is generated just looking at the code. Of course, if the JS relies on some PHP variables, they need to be instanciated before the JS is output.

<?php
$test = 'Hello world';
?>
<html>
    <body>
        <script>
            alert('<?php echo $test; ?>');
        </script>
    </body>
</html>

will work but

<html>
    <body>
        <script>
            alert('<?php echo $test; ?>');
        </script>
    </body>
</html>
<?php
$test = 'Hello world';
?>

will not

share|improve this answer

Your using @$_POST indicates that you have received (or are expecting) errors - check your generated source to see if the value was output correctly. Otherwise document.getElementById will fail and you'd get no output.

share|improve this answer
 alert("Delete entry <? echo $row['id']; ?> ")
share|improve this answer

If your extension is js, php will not work in that file.

The reason being, php parses on files that it is supposed to. The file types that php will parse are configured in httpd.conf using AddType commands (or directives, whatever they are called).

So you have 3 options:

  • add filetype js to the list of files php will parse (BAD, VERY BAD)
  • make the script inline to some php file
  • rename the file to script.js.php, and at the beginning of the file, specify the content type, like so:

    <?php header( 'content-type: text/javascript' ); ?>

Cheers!

share|improve this answer
1  
Or a compromise, put the data in a varaible through an inline <script> element in the PHP, and have the external static .js file pick it up. You generally don't want to be serving script files through PHP as unless you put a lot of effort into setting the right headers you'll break the cacheing and increase the network and server load. – bobince Jan 4 '10 at 11:13

Use json_encode to convert some text (or any other datatype) to a JavaScript literal. Don't just put quotes around the echoed string — what if the string has a quote in it, or a newline, or backslash? Best case your code fails, worst case you've got a big old cross-site-scripting security hole.

So,

<?php
    function js($o) {
        echo json_encode($o, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);
    }
?>
<script type="text/javascript">
    var areaOption= document.getElementById(<?php js($_POST['annonsera_name']); ?>);
    areaOption.selected= true;
    alert (<?php js('Hello World'); ?>);
</script>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.