简体   繁体   中英

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

 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

Does your assignment requires 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. I think this will be easier than using 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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM