表单操作无效

时间:2014-03-30 18:59:19

标签: javascript php jquery html

我有这个HTML代码:

<form action="connect.php" method="get" name="registerForm">
            <h3>Not a member yet? Sign Up!</h3>
            <p>Username:</p><input type="text" name="name" id="registerinput_username">
                <div id="usernamereq2"><p>Field is required.</p><img src="ximg.png" alt="x"></div>
            <p>Email:</p><input type="text" name="email" id="registerinput_email">
                <div id="emailreq"><p>Field is required.</p><img src="ximg.png" alt="x"></div>
            <p>A password will be e-mailed to you.</p>
            <input type="submit" name="submit" value="Register" id="registersubmit">
</form>

然后我运行一个php函数在我的数据库中发布输入:

<?php
$con=mysqli_connect("localhost","root","","photoalbum");

$username=$_GET['name'];
$email=$_GET['email'];

$sql="INSERT INTO registration (UserName, Email)
VALUES ('$username', '$email')";

mysqli_close($con);
?>

但是,由于我也在同时运行javascript函数来检查输入是否有效,因此connect.php不会运行。

这是javascript:

function validateFormRegister()
    {
        var email=document.forms["registerForm"]["email"].value;
        var atpos=email.indexOf("@");
        var dotpos=email.lastIndexOf(".");
        var name=document.forms["registerForm"]["name"].value;
        var x=name.length;
            if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
                {
                    alert("Email is invalid.");
                }
            if(x<5){
                    alert("Username is too short! Must be at least 5 characters.");
                }
            if(atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length && x<5)
            {
            return false;
            }
    }

2 个答案:

答案 0 :(得分:1)

将表单的action参数设置为PHP脚本,并在JavaScript事件onsubmit中执行任何JavaScript处理:

<form action="connect.php" method="get" name="registerForm" 
onsubmit="return validateFormRegister()">

提交表单时,它将触发JavaScript函数,然后验证输入字段并返回布尔结果(true或false)。表单提交仅在返回值为true时发生,在这种情况下,值将发送到PHP脚本(connect.php)以进行进一步处理。

另请注意,您的查询容易受到SQL injection的攻击。我建议您了解prepared statements并在代码中使用它,以便您的代码免受SQL注入攻击。

答案 1 :(得分:-1)

将这些更改为(您缺少$ result = $ con&gt;查询($ sql))

$con=mysqli_connect("localhost","root","","photoalbum");    
$username=$_GET['name'];
$email=$_GET['email'];
$sql="INSERT INTO registration (UserName, Email)
VALUES ('$username', '$email')";
$result = $con->query($sql);
mysqli_close($con);
?>

function validateFormRegister()
    {
        var email=document.forms["registerForm"]["email"].value;
        var atpos=email.indexOf("@");
        var dotpos=email.lastIndexOf(".");
        var name=document.forms["registerForm"]["name"].value;
        var x=name.length;
            if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
                {
                    alert("Email is invalid.");
                }
            if(x<5){
                    alert("Username is too short! Must be at least 5 characters.");
                }
            if(atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length && x<5)
            {
            return false;
            }
    return true;
    }

<form action="connect.php" method="get" name="registerForm" onsubmit="return validateFormRegister()">