簡體   English   中英

PHP內的Java回聲未執行?

[英]Javascript inside php echo not executing?

<?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的<p>元素的內容。 我嘗試到處找我的答案,很抱歉,如果這是重復的,但任何幫助將不勝感激...

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

“您的基本問題是它正在執行,但是您的文檔尚未加載。在您的javascript正在加載和執行時,沒有帶有警告ID的元素”〜Watcher

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM