Javascript代码在php变量中定义不起作用

时间:2014-05-17 06:54:33

标签: javascript php

我是java脚本的新手。我试着调用一个在php变量中定义的javascript代码。但是当我回显那个特定的php变量时,它实际上并没有做我的javascript代码必须做的事情。 这是我的代码

<?php

if(isset($_SESSION['subscribe_email']) && $_SESSION['subscribe_email']!=""){
    logw("inside_if".$_SESSION['subscribe_email']);
if ($_SESSION['subscribe_email'] === 'SUCCESS') {
    $subscribe_session_value = "<script type='text/javascript' charset='utf-8'>
                                        $(document).ready(function() {
                                        $('#id_subscribe_button').text('Successfully Subscribed');

            $('#exampleInputEmail2').hide();
            alert('success');
            </script>

";

    echo $subscribe_session_value;
} else if ($_SESSION['subscribe_email'] === 'ALREADY_EXISTING') {
    $subscribe_session_value = "<script type='text/javascript' charset='utf-8'>
                                        $(document).ready(function() {
                                        $('#id_subscribe_button').text('Already Subscribed');

            $('#exampleInputEmail2').hide();
             alert('Already Subscribed');
            </script>

";

     echo $subscribe_session_value;
}
}
?>

1 个答案:

答案 0 :(得分:1)

您的$(document).ready功能未正确关闭意味着缺少});尝试此

if ($_SESSION['subscribe_email'] === 'SUCCESS') {
    $subscribe_session_value = "<script type='text/javascript' charset='utf-8'>
                                        $(document).ready(function() {
                                            $('#id_subscribe_button').text('Successfully Subscribed');

                                            $('#exampleInputEmail2').hide();
                                            alert('success');
                                        }); 
                                </script>
";

    echo $subscribe_session_value;
} else if ($_SESSION['subscribe_email'] === 'ALREADY_EXISTING') {
    $subscribe_session_value = "<script type='text/javascript' charset='utf-8'>
                                        $(document).ready(function() {
                                            $('#id_subscribe_button').text('Already Subscribed');

                                            $('#exampleInputEmail2').hide();
                                            alert('Already Subscribed');
                                        });
                                </script>

";

     echo $subscribe_session_value;
}