简体   繁体   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. 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. It was working fine but after i restarted my computer it just stoped working no error no nothing just does not work, the only way to make it work is if i change some variable names from the html, javascript and php, ihave tried so many thing like restarting my wamp server or restarting again make a new php file using in line javascript but its all the same, i'm just tryin not to pull my hair out at this point, thanks in advance if anyone can figure out whats wrong with the code它工作正常,但是在我重新启动计算机后,它只是停止工作,没有错误,没有任何东西不起作用,使它工作的唯一方法是如果我从 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>

Sorry For the PHP error because i have no idea how to insert php command on this site抱歉 PHP 错误,因为我不知道如何在此站点上插入 php 命令

Does your assignment requires javascript?您的作业是否需要 javascript? If I'm like you I will do it like this:如果我像你一样,我会这样做:

   <?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. 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. I think this will be easier than using javascript.我认为这会比使用 javascript 更容易。

I just took a brief look on the code and fixed some issues that were pointed out by the console.我只是简单地看了一下代码并修复了控制台指出的一些问题。 The most confusing part is transmitting $_POST[] variables by including them in your $sql.最令人困惑的部分是通过将 $_POST[] 变量包含在 $sql 中来传输它们。 It did not throw any further errors and I hope this helps:它没有引发任何进一步的错误,我希望这会有所帮助:

<?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