php variable in javascript
I have the following code in php:
$test = 'abc';
echo '<script type="text/javascript">\n';
echo 'movtitle = ' . $test . ';\n';
echo '</script>';
and I tried to do an alert(movtitle)开发者_如何学C
, but it doesn't print out anything. What am I doing wrong?
I'd do it this way:
<?php
$test = 'abc';
?>
<script type="text/javascript">
alert('<?php echo $test; ?>');
</script>
You are missing the quotes around the value of movtitle:
echo "movtitle = '" . $test . "';\n";
You are solving this problem wrong way, upside down.
There are no PHP variables in javascript at all. There is only some string you have to output in PHP.
So, the right way would be:
Get to know, what javascript code you want to have. Ask here or google for it and then edit it to make it fit your needs (add 'abc' to alert in this case)
test, if it works.Write PHP code that builds the same JS you wrote manually.
3. See what you get. Most important part. Do not watch PHP code. Always watch resulting JS code. Find the differences and then correct PHP code to eliminate them. It's no more than just simple string operations.
Edit: it seems you failed at opt. 1. Do you have pure JS code working? It seems there is some problem with your browser, not PHP
There are a couple issues at play here:
First, you're using single quotes for your echo statements, so your escape characters aren't working. The /n
characters are being echoed literally, so your output looks like this:
<script type="text/javascript">\nmovtitle = abc;\n</script>
You need double quotes for the escaping to work, so you can either swap quotes (i.e. double on the outside, single on the inside) or just escape the double quotes themselves:
Option 1:
echo "<script type='text/javascript'>\n";
echo "movtitle = '" . $test . "';\n";
echo "</script>";
Option 2:
echo "<script type=\"text/javascript\">\n";
echo "movtitle = \"" . $test . "\";\n";
echo "</script>";
Your second problem is that you're not surrounding the "abc" value with quotes. The output, as shown above, results in the following JavaScript line:
movtitle = abc;
This is saying "assign the value of the variable "abc" to the variable "movtitle". Since there is no variable "abc" in the code, the line fails. The line needs to be output like this:
movtitle = "abc";
Note that my example code above fixes this issue for you.
An even better option would be to not echo this with PHP at all. Instead, drop out of PHP and write your raw HTML directly. You can hop into PHP quickly just to drop in the variable:
<?php
// normal processing
$test = 'abc';
?>
<script type="text/javascript">
movtitle = " <?php echo $test; ?>";
</script>";
<?php
// resume processing
?>
In general, it's better to keep your PHP processing work and your HTML output as separate as possible. Do all your work upfront in PHP, then include
a template file with the same sort of "fill-in-the-blanks" PHP variable use as I show in my last example.
you migh try this:
echo 'movtitle = "' . $test . '";\n';
which will create javascript that looks like this movtitle = "Cassablanca";
Note also that the \n will not work inside the single quotes.
An it would be even a good idea to JSON encode the variable:
$test = 'abc';
print("<script type=\"text/javascript\">\n");
printf("movtitle = JSON.parse('%s');\n", json_encode($test));
print("</script>");
With this solution you can pass multiple object types to javascript.
精彩评论