通过参数将php变量传递给javascript函数PHP,JAVASCRIPT [英] Passing php variables through the parameters to a javascript function, PHP, JAVASCRIPT

查看:237
本文介绍了通过参数将php变量传递给javascript函数PHP,JAVASCRIPT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串变量通过名为alertText的php函数的参数传递给名为alert的javascript函数,以将消息提示给屏幕,但是由于某种原因它不接受php变量,因此什么也没有被提醒到屏幕.请看一下:

I'm trying to pass a string variable through the parameter of a php function called alertText to a javascript function called alert, to alert a message to the screen, but it's not accepting the php variable for some reason, and nothing is being alerted to the screen. Please take a look:

testing.php

testing.php

function alertText($text) {

echo "

<script>
alert($text);
</script>

";

}

alertText("Hello");

?>

推荐答案

您需要单引号$ text变量,因为JavaScript无法将该变量识别为字符串.

You need to single-quote the $text variable, because JavaScript is not recognizing that variable as string.

<?php 
function alertText($text) {
    echo "<script>alert('$text');</script>";
}
alertText("Hello");

这篇关于通过参数将php变量传递给javascript函数PHP,JAVASCRIPT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆