php内部的Javascript echo不执行?

时间:2015-11-06 17:00:19

标签: javascript php html

<?php
//Clear the cache for form values

header('Cache-Control: no store, no-cache, must-revalidate');
header('Cache-Control: no store, no-cache, must-revalidate');
header('Pragma: no-cache');

//Load up functions
include "recon.php";


//Create the PHP Self Function
$username = $nohours = $noitem = $ipaddr = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);
    $nohours = filter_input(INPUT_POST, "hours", FILTER_SANITIZE_NUMBER_INT);
    $noitem = 0;
    $xnoitem = filter_input(INPUT_POST, "itemselect", FILTER_SANITIZE_STRING);
    if ($xnoitem === "item1") {
        $noitem = 1;
    }
    $ipaddr = get_client_ip();

    if(!empty($username)){
        echo "Username is not null";
    }
    else{
        echo '<script type="text/javascript">document.getElementById("warning").innerHTML = "New text!";</script>';
    }
}
?>

<!DOCTYPE HTML>
<html>
    <head>

        <title> GGG Time Form </title>
        <script type="text/javascript">
            function outputUpdate(hours)
            {
                document.querySelector('#nohours').value = hours;

                document.getElementById("total").innerHTML = "$" + hours * 3.75;

                document.getElementById("item1").checked = false;
            }

            function xitem1()
            {
                document.getElementById("total").innerHTML = "$16";
            }
        </script>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <div>
            <div>
                <h1 class="ctext">GGG Time Order Form</h1>
            </div>
            <div>
                <p id="warning"></p>
            </div>
        </div>
        <hr>

        <div>
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                (Enter your username you login with at GGG) Username:<span style="color:red;">*</span> <input type="text" name="username">
                <br>
                <br>
                <label for="hourslider">Number of hours you would like to purchase: </label>
                <input type="range" name="hours" min="1" max="24" value="12" id="hourslider" oninput="outputUpdate(value)">
                <output for="hourslider" id="nohours">12</output>
                <br>
                <br>
                <h3> Or select one of our packages below </h3>
                <br>
                <input type="radio" name="itemselect" value="item1" id="item1" onclick="xitem1()"> Day Pass $16.00
                <hr>
                <h3> Total payment owed: <p id="total">$0</p></h3>
                <input type="submit">
                <hr>
            </form>
        </div>
        <div id="ipbox">
            <span>For security purposes your ip address is being logged: <?php echo get_client_ip(); ?></span>
        </div>

        <div>
        </div>

    </body>
</html>

所以在这个文本下面的逻辑语句中(也位于上面的代码中)我想知道为什么当我回显javascript时它没有改变id为warning的<p>元素的内容。我已经尝试到处寻找我的答案,如果这是重复的话我很抱歉,但任何帮助都会受到赞赏......

if(!empty($username)){
        echo "Username is not null";
    }
    else{
        echo '<script type="text/javascript">document.getElementById("warning").innerHTML = "New text!";</script>';
    }

1 个答案:

答案 0 :(得分:1)

“你的基本问题是它正在执行,但你的文档都没有加载。在你的javascript加载和执行时,没有任何元素的警告ID”~Watcher