使用javascript在表单验证后注册失败

时间:2017-05-29 13:09:12

标签: javascript php

我是java脚本编码的新手。我有一个签名表格,其中包含用户名,密码和电子邮件。它会验证其中的三个,并且不会进入下一个“logout.php”页面,如果已经使用了id,则甚至不会发出警报。它只验证文本框的条目。我想首先验证表单中的三个条目然后如果它们是有效的并且id是唯一的,那么单击提交“logout.php”应该打开。 以下是我的代码:

登录-up.php

<form method="post" name='registration' onSubmit="return formValidation();">

<input type="text" placeholder="Id*" required=" "  name="uid">

<input type="password" placeholder="Password*" required=" " name="passid">

<input type="text" placeholder="Emailaddress*" required=" " name="uemail">

<input type="submit" value="Submit" style="font-face: 'Comic Sans MS'; font-size: larger; color: white; background-color: #FFA500; border: 3pt ridge lightgrey">
                        </form>

用于验证的Java脚本代码:

的javascript

<script type="text/javascript">
function formValidation()
{
var uid = document.registration.uid;
var passid = document.registration.passid;
var uemail = document.registration.uemail;


if(userid_validation(uid,5,12))
{

if(passid_validation(passid,7,12))
{

if(ValidateEmail(uemail))
{
}
}
}
return false;
}


function userid_validation(uid,mx,my)
{
var uid_len = uid.value.length;
if (uid_len == 0 || uid_len >= my || uid_len < mx)
{
alert("User Id should not be empty / length be between "+mx+" to "+my);
uid.focus();
return false;
}
return true;
}

function passid_validation(passid,mx,my)
{
var passid_len = passid.value.length;
if (passid_len == 0 ||passid_len >= my || passid_len < mx)
{
alert("Password should not be empty / length be between "+mx+" to "+my);
passid.focus();
return false;
}
return true;
}


function ValidateEmail(uemail)
{
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(uemail.value.match(mailformat))
{
//alert("You have entered a valid email address!");
return true;
}
else
{
alert("You have entered an invalid email address!");
uemail.focus();
return false;
}
}

PHP部分检查是否已经使用了id。单击提交按钮后,此部分代码未执行。这段代码中没有显示任何消息。

PHP

<?php
session_start();
if(!empty($_POST)) {
    class MyDB extends SQLite3
    {
        function __construct()
        {
            $this->open('trip.db');
        }
    }
    $db = new MyDB();
    if(!$db){
        echo $db->lastErrorMsg();
    } else {

    }
    $id=null;
    $pass=null;
    $email=null;
    $id_exists=false;

    if (isset($_POST['uid'])) {
        $id = $_POST['uid'];
    }

    if (isset($_POST['passid'])) {
        $pass = $_POST['passid'];
    }

      if (isset($_POST['uemail'])) {
        $email = $_POST['uemail'];
    }



    $result= "SELECT COUNT(*) FROM Users WHERE ID = '".$id. "';" ;
    $count= $db->querySingle($result);

    if ($count > 0)
    {
        $id_exists = true;
        echo "This id is not available. Please enter a valid id. ";
    }

    else
    {
        $sql= " INSERT INTO Users (ID, PASSWORD, EMAIL)
          VALUES ('$id','$pass','$email'); " ;
        $ret = $db->query($sql);
        $_SESSION['Id'] = $id;
        header("location:logout.php");
    }


    $db->close();
}
?>  

1 个答案:

答案 0 :(得分:1)

这是示例代码。

 function test_js() {
 if(document.test_form.id.value == "") {
  alert('input name!');
  document.test_form.id.focus();
  return;
 } else {
  document.test_form.action = "logout.php";
  document.test_form.submit();
 }
}
<form method="post" action="login.php" name="test_form">
        <input type="text" name="id">
        <input type="submit" value="Submit" onclick="javascript:test_js();">
</form>