如何在PHP中添加javascript代码? [重复]

时间:2021-01-17 00:14:55

This question already has an answer here:

这个问题在这里已有答案:

I want to add script code in php but it giving error on this line

我想在php中添加脚本代码,但它在这一行上给出了错误

$a=echo "document.getElementById('t1').value;";

of unexpected use of ECHO! Can any one help???

意外使用ECHO!任何人都可以帮忙???

<?php
    echo "<script>";
    $a=echo "document.getElementById('t1').value;";


    if($_session['user']==$a) {
        echo 'function fun() {
            document.write("welcom");
        }';
    }

    echo "</script>";
?>

2 个解决方案

#1


Your problem is from $a=echo because echo is a php function.

你的问题来自$ a = echo,因为echo是一个php函数。

The correct syntax is:

正确的语法是:

<?php
$a= "document.getElementById('t1').value;";
echo "<script>";
echo $a;
if($_session['user']==$a) {
    echo 'document.write("welcom");';
}
echo "</script>";
?>

#2


You cant use echo function in variable. You can echo variable only and there are 2 ways, how to achieve this:

你不能在变量中使用echo函数。你可以只回显变量,有两种方法,如何实现这个:

1) you can save the content into variable via:

1)您可以通过以下方式将内容保存到变量中:

$a = "document.getElementById('t1').value;"; 

and somewhere in your code where you will need it you can echo the content of $a variable via: echo $a;

在您需要它的代码中,您可以通过以下方式回显$ a变量的内容:echo $ a;

2) you can echo the content of your variable in the concrete line:

2)您可以在具体行中回显变量的内容:

echo "document.getElementById('t1').value;";

#1


Your problem is from $a=echo because echo is a php function.

你的问题来自$ a = echo,因为echo是一个php函数。

The correct syntax is:

正确的语法是:

<?php
$a= "document.getElementById('t1').value;";
echo "<script>";
echo $a;
if($_session['user']==$a) {
    echo 'document.write("welcom");';
}
echo "</script>";
?>

#2


You cant use echo function in variable. You can echo variable only and there are 2 ways, how to achieve this:

你不能在变量中使用echo函数。你可以只回显变量,有两种方法,如何实现这个:

1) you can save the content into variable via:

1)您可以通过以下方式将内容保存到变量中:

$a = "document.getElementById('t1').value;"; 

and somewhere in your code where you will need it you can echo the content of $a variable via: echo $a;

在您需要它的代码中,您可以通过以下方式回显$ a变量的内容:echo $ a;

2) you can echo the content of your variable in the concrete line:

2)您可以在具体行中回显变量的内容:

echo "document.getElementById('t1').value;";