簡體   English   中英

JavaScript 和 php 在一段時間后停止工作,只有在我重命名一些變量時才有效

[英]JavaScript and php stops working after some time and only works if i rename some variables

A part of my assignment is to create registration system using php and mysql and did that and used javascript to check if the input values are valid before submitting it to the php code where it can run the query in mysql. 它工作正常,但是在我重新啟動計算機后,它只是停止工作,沒有錯誤,沒有任何東西不起作用,使它工作的唯一方法是如果我從 html、javascript 和 ZE1BFD762321E409CEE4AC0B6E8 更改一些變量名,我嘗試了很多東西就像重新啟動我的 wamp 服務器或再次重新啟動一樣,使用 javascript 行創建一個新的 php 文件,但它們都是一樣的,我只是盡量不要在這一點上拔頭發,如果有人能找出問題所在,請提前感謝代碼

 function Data_Check() { var mail = document.getElementById('EmailCh').value; var nam = document.getElementById('nameC').value; var pass = document.getElementById('passC').value; var dob = document.getElementById('DOBC').value; var poa = document.getElementById('POAC').value; var poc = document.getElementById('POCC').value; var foram = document.getElementById('logForm'); if (mail === "" || nam === "" || pass === "" || dob === "" || poa === "" || poc === "") { if (mail === "") { document.getElementById('EmailInvalid').style.display = "block"; } if (nam === "") { document.getElementById('nameInvalid').style.display = "block"; } if (pass === "") { document.getElementById('PasswordInvalid').style.display = "block"; } if (dob === "") { document.getElementById('DOBInvalid').style.display = "block"; } if (poa === "") { document.getElementById('POAInvalid').style.display = "block"; } if (poc === "") { document.getElementById('POCInvalid').style.display = "block"; } } else { document.getElementById('submitt').submit(); } } function Restrt(Parame) { document.getElementById(Parame).style.display = "none"; }
 body { background-image: url(../Images/PatternBack.jpg); margin: 0; top: 0; padding-top: 0; }.borCheck:invalid { border: 1.5px solid #FF5C5C; }.borCheck:Valid { border: 1.5px solid green; }.invalid { color: #FF5C5C; font-weight: bold; position: absolute; float: right; display: none; }.First-cont { text-align: center; background-image: url(../Images/NavBack.jpg); margin: 3% 35% 0% 35%; padding: 0% 5% 3% 5%; border-radius: 2%; border: 2px solid black; }.second-cont { text-align: left; }.second-cont label { display: inline-block; width: 300px; font-size: 100%; }.second-cont input { display: inline-block; width: 100%; height: 30px; border-radius: 3%; outline: none; }.signupLet { padding: 0 0 10% 0; font-size: 1.5em; }.signupLet span { display: inline-block; }.reg { display: inline-block; width: 100%; padding: 10% 0%; font-size: 1.5em; }.second-cont button { color: white; background-color: #4CAF50; border: 1px solid black; transition: 0.2s; border-radius: 2px; }.second-cont button:hover { background-color: white; color: black; border: 1px solid black; transition: 0.2s; border-radius: 2px; }.loginD { background-image: url(../Images/pathersign.png); text-align: center; margin: 10% 35% 0 35%; padding: 0% 5% 2% 5%; border: 1px solid black; border-radius: 2%; }.loginD label { float: left; width: 100%; }.loginD input { display: inline-block; width: 100%; outline: NONE; padding: 8px; }.SubBut { color: white; background-color: black; border: 1px solid white; transition: 0.2s; border-radius: 2px; padding: 20px; }.SubBut:hover { background-color: white; color: black; border: 1px solid black; transition: 0.2s; border-radius: 2px; }
 <body> <?php Include "Header.php"; ?> <?php session_start(); ?> <div class="First-cont"> <div class="signupLet"> <img src="Images/signupuser.png" alt="" width="50%" height="auto"><br> <span style="color:white;">SIGN UP</span> <div> <?php echo $_SESSION["incorrect"]; ?> </div> </div> <div class="second-cont"> <form action="" <?php echo $_SERVER[ 'PHP_SELF']; ?>"" method="post" name="register" id="submitt" > <label>Email Address:</label> <input class="borCheck" type="email" name="EmailAddress" placeholder="Email Address" id="EmailCh" onkeypress="Restrt('EmailInvalid')" /> <span class="invalid" id="EmailInvalid">Please type a Valid Email Address</span> <br><br> <label>Username:</label> <input class="borCheck" type="text" name="Uname" id="nameC" onkeypress="Restrt('nameInvalid')" /> <span class="invalid" id="nameInvalid">Please type a Valid name</span> <br><br> <label>Password:</label> <input class="borCheck" type="password" name="Pass" id="passC" onkeypress="Restrt('PasswordInvalid')" /> <span class="invalid" id="PasswordInvalid">Please type a Valid Password</span> <br><br> <label>Date Of Birth:</label> <input class="borCheck" type="Date" name="DOB" id="DOBC" onkeypress="Restrt('DOBInvalid')" /> <span class="invalid" id="DOBInvalid">Date of birth is Required</span> <br><br> <label>Postal Address:</label> <input class="borCheck" type="text" name="POAddress" id="POAC" onkeypress="Restrt('POAInvalid')" /> <span class="invalid" id="POAInvalid">Please Type a Valid Postal address</span> <br><br> <label>Postal Code:</label> <input class="borCheck" type="number" name="POCode" id="POCC" onkeypress="Restrt('POCInvalid')" /> <span class="invalid" id="POCInvalid">Please type A Valid Postal Code</span> <br><br> </form> <button onclick="Data_Check()" name="Sub" class="reg">Submit</button> </div> </div> <?php if (isset($_POST['Sub'])) { // Create connection $conn = new mysqli('localhost','root',''); // Check connection if ($conn->connect_error) { die("Connection failed: ". $conn->connect_error); } echo "DB Connected successfully"; // this will select the Database sample_db mysqli_select_db($conn,"E-Cycle"); echo "\n DB is seleted as Test successfully"; // create INSERT query $sql="INSERT INTO UserLogin (EmailAddress,Uname,Pass,DOB,POAddress,POCode) VALUES ('$_POST[EmailAddress]','$_POST[Uname]','$_POST[Pass]','$_POST[DOB]','$_POST[POAddress]','$_POST[POCode]')"; if ($conn->query($sql) === TRUE) { header('Location: http://localhost/E-Cycle/Home.php'); } else { $_SESSION["incorrect"]="Email Already Exists"; header('Location: Signup.php'); } mysqli_close($conn); }?> </body>

抱歉 PHP 錯誤,因為我不知道如何在此站點上插入 php 命令

您的作業是否需要 javascript? 如果我像你一樣,我會這樣做:

   <?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(email_exists($email)) {
        //email is already in use
    } elseif (nickname_exists($nickname)) {
        //nickname already exists
    } elseif ($password != $confirm_password) {
        //password does not match
    }  else {
    //insert into database if everything is ok
    }
    }
?>

Then I will create a function which will set a message into a session and one more function to show the message and clear the session where the message is saved. 我認為這會比使用 javascript 更容易。

我只是簡單地看了一下代碼並修復了控制台指出的一些問題。 最令人困惑的部分是通過將 $_POST[] 變量包含在 $sql 中來傳輸它們。 它沒有引發任何進一步的錯誤,我希望這會有所幫助:

<?php
Include "Header.php";
session_start();
?>
<head>
    <script>
        function Data_Check() {
            var mail = document.getElementById('EmailCh').value;
            var nam = document.getElementById('nameC').value;
            var pass = document.getElementById('passC').value;
            var dob = document.getElementById('DOBC').value;
            var poa = document.getElementById('POAC').value;
            var poc = document.getElementById('POCC').value;
            var foram = document.getElementById('logForm');
            if (mail === "" || nam === "" || pass === "" || dob === "" || poa === "" || poc === "") {
                if (mail === "") {
                    document.getElementById('EmailInvalid').style.display = "block";
                }
                if (nam === "") {
                    document.getElementById('nameInvalid').style.display = "block";
                }
                if (pass === "") {
                    document.getElementById('PasswordInvalid').style.display = "block";
                }
                if (dob === "") {
                    document.getElementById('DOBInvalid').style.display = "block";
                }
                if (poa === "") {
                    document.getElementById('POAInvalid').style.display = "block";
                }
                if (poc === "") {
                    document.getElementById('POCInvalid').style.display = "block";
                }
            }
            else {
                document.getElementById('submitt').submit();
            }
        }
        function Restrt(Parame) {
            document.getElementById(Parame).style.display = "none";
        }
    </script>
    <style>
        body {
            background-image: url(../Images/PatternBack.jpg);
            margin: 0;
            top: 0;
            padding-top: 0;
        }
        .borCheck:invalid {
            border: 1.5px solid #FF5C5C;
        }
        .borCheck:Valid {
            border: 1.5px solid green;
        }
        .invalid {
            color: #FF5C5C;
            font-weight: bold;
            position: absolute;
            float: right;
            display: none;
        }
        .First-cont {
            text-align: center;
            background-image: url(../Images/NavBack.jpg);
            margin: 3% 35% 0% 35%;
            padding: 0% 5% 3% 5%;
            border-radius: 2%;
            border: 2px solid black;
        }
        .second-cont {
            text-align: left;
        }
        .second-cont label {
            display: inline-block;
            width: 300px;
            font-size: 100%;
        }
        .second-cont input {
            display: inline-block;
            width: 100%;
            height: 30px;
            border-radius: 3%;
            outline: none;
        }
        .signupLet {
            padding: 0 0 10% 0;
            font-size: 1.5em;
        }
        .signupLet span {
            display: inline-block;
        }
        .reg {
            display: inline-block;
            width: 100%;
            padding: 10% 0%;
            font-size: 1.5em;
        }
        .second-cont button {
            color: white;
            background-color: #4CAF50;
            border: 1px solid black;
            transition: 0.2s;
            border-radius: 2px;
        }
        .second-cont button:hover {
            background-color: white;
            color: black;
            border: 1px solid black;
            transition: 0.2s;
            border-radius: 2px;
        }
        .loginD {
            background-image: url(../Images/pathersign.png);
            text-align: center;
            margin: 10% 35% 0 35%;
            padding: 0% 5% 2% 5%;
            border: 1px solid black;
            border-radius: 2%;
        }
        .loginD label {
            float: left;
            width: 100%;
        }
        .loginD input {
            display: inline-block;
            width: 100%;
            outline: NONE;
            padding: 8px;
        }
        .SubBut {
            color: white;
            background-color: black;
            border: 1px solid white;
            transition: 0.2s;
            border-radius: 2px;
            padding: 20px;
        }
        .SubBut:hover {
            background-color: white;
            color: black;
            border: 1px solid black;
            transition: 0.2s;
            border-radius: 2px;
        }
    </style>
</head>
<body>
    <div class="First-cont">
        <div class="signupLet">
            <img src="Images/signupuser.png" alt="" width="50%" height="auto">
            <br>
            <span style="color:white;">SIGN UP</span>
            <div>
                <?php
echo $_SESSION["incorrect"];
                ?>
            </div>
        </div>
        <div class="second-cont">
            <form action="
<?php echo '$_SERVER["PHP_SELF"]'; ?>" method="post" name="register" id="submitt">
                <label>Email Address:</label>
                <input class="borCheck" type="email" name="EmailAddress" placeholder="Email Address" id="EmailCh" onkeypress="Restrt('EmailInvalid')" />
                <span class="invalid" id="EmailInvalid">Please type a Valid Email Address</span>
                <br>
                <br>
                <label>Username:</label>
                <input class="borCheck" type="text" name="Uname" id="nameC" onkeypress="Restrt('nameInvalid')" />
                <span class="invalid" id="nameInvalid">Please type a Valid name</span>
                <br>
                <br>
                <label>Password:</label>
                <input class="borCheck" type="password" name="Pass" id="passC" onkeypress="Restrt('PasswordInvalid')" />
                <span class="invalid" id="PasswordInvalid">Please type a Valid Password</span>
                <br>
                <br>
                <label>Date Of Birth:</label>
                <input class="borCheck" type="Date" name="DOB" id="DOBC" onkeypress="Restrt('DOBInvalid')" />
                <span class="invalid" id="DOBInvalid">Date of birth is Required</span>
                <br>
                <br>
                <label>Postal Address:</label>
                <input class="borCheck" type="text" name="POAddress" id="POAC" onkeypress="Restrt('POAInvalid')" />
                <span class="invalid" id="POAInvalid">Please Type a Valid Postal address</span>
                <br>
                <br>
                <label>Postal Code:</label>
                <input class="borCheck" type="number" name="POCode" id="POCC" onkeypress="Restrt('POCInvalid')" />
                <span class="invalid" id="POCInvalid">Please type A Valid Postal Code</span>
                <br>
                <br>
            </form>
            <button onclick="Data_Check()" name="Sub" class="reg">Submit</button>
        </div>
    </div>
    <?php
// Create connection
if (isset($_POST['Sub'])) {
    $conn = new mysqli('localhost','root','');
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "DB Connected successfully";
    // this will select the Database sample_db
    mysqli_select_db($conn,"E-Cycle");
    echo "\n DB is seleted as Test  successfully";
    // create INSERT query

    $email = $_POST['EmailAdress'];
    $uname = $_POST['Uname'];
    $pass = $_POST['Pass'];
    $dob = $_POST['DOB'];
    $poadress = $_POST['POAdress'];
    $pocode = $_POST['POCode'];

    $sql = "INSERT INTO UserLogin (EmailAddress, Uname,Pass, DOB, POAddress, POCode) VALUES ($email, $uname, $pass, $dob, $poadress, $pocode)";

    if ($conn->query($sql) === TRUE)
    {
        header('Location: http://localhost/E-Cycle/Home.php');
    }
    else
    {
        $_SESSION["incorrect"]="Email Already Exists";
        header('Location: Signup.php');
    }
    mysqli_close($conn);
}
    ?>
</body>

暫無
暫無

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

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