简体   繁体   English

JavaScript验证无法插入数据库

[英]JavaScript Validation cannot insert into the database

okay i had to rephrase my question, my javascript codes cannot proceed to the next step which is isset['submit'] it just stuck there. 好吧,我不得不重新表述我的问题,我的JavaScript代码无法继续进行下一步,即isset ['submit']它只是卡在了那里。 im really hoping someone here can help my correct my javascript code so that it will successfully call the isset and run my php code to add it to the database. 我真的希望有人在这里可以帮助我纠正我的JavaScript代码,以便它将成功调用isset并运行我的php代码以将其添加到数据库中。

my php code 我的PHP代码

if(isset($_POST['submit'])){

$firstname = mysql_escape_string($_POST['fname']);
$lastname = mysql_escape_string($_POST['lname']);
$age = mysql_escape_string($_POST['age']);
$gender = mysql_escape_string($_POST['gender']);
$email = mysql_escape_string($_POST['email']);
$password = mysql_escape_string($_POST['password']);
$nickname = mysql_escape_string($_POST['nickname']);
$regflag = 0;



$sql = "SELECT * FROM accounts";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{

        if($row['email'] == $email)
        {
            echo '<b> The email is already taken!</b>'; 
            $regflag = 0;
        }
        if($row['nickname'] == $nickname)
        {
            echo '&nbsp;<b>The nickname is already taken!</b>'; 
            $regflag = 0;
        }
}

if($regflag == 1)
{

    $regsql = "INSERT INTO accounts         VALUES('','$firstname','$lastname','$age','$gender','$email','$password','$nickname','MEMBER',now())";
    $query = mysql_query($regsql);
    echo '<body onLoad = "regsucess()"> </body>';
}

} }

my javascript code 我的JavaScript代码

<script type = "text/javascript">
function validator(){

    if(!document.register.fname.value)
    {
    alert('You need to supply your first name.');
    document.register.fname.focus();
    return false;
    }
    if(!document.register.lname.value)
    {
    alert('You need to supply your last name.');
    document.register.lname.focus();
    return false;
    }
    if(!document.register.age.value)
    {
    alert('You need to supply your age.');
    document.register.age.focus();
    return false;
    }
    if(!document.register.email.value)
    {
    alert('You need to supply your email.');
    document.register.email.focus();
    return false;
    }
    if(!document.register.password.value)
    {
    alert('You need to supply your password.');
    document.register.password.focus();
    return false;
    }
    if(!document.register.nickname.value)
    {
    alert('You need to supply your nickname.');
    document.register.nickname.focus();
    return false;
    }


register.action = "register.php"
document.register.submit();
//alert("Congrats!");


}

Why register.action = "register.php" and not 为什么register.action = "register.php"而不是
document.register.action = "register.php" ?? document.register.action = "register.php" ??
also if you call this from onclick of a submit button or from onsubmit, then remove the last line document.register.submit(); 同样,如果您是通过提交按钮的onclick或onsubmit调用的,则删除最后一行document.register.submit();

Lastly if ANYTHING in your form has name="submit" you will not be able to call the submit event from script 最后,如果您表单中的任何内容都具有name="submit"您将无法从脚本中调用Submit事件

Here is what I would expect to see 这是我期望看到的

<script type = "text/javascript">
function validate(theForm){

  if(!theForm.fname.value) {
    alert('You need to supply your first name.');
    theForm.fname.focus();
    return false;
  }
  if(!theForm.lname.value) {
    alert('You need to supply your last name.');
    theForm.lname.focus();
    return false;
  }
  if(!theForm.age.value) {
    alert('You need to supply your age.');
    theForm.age.focus();
    return false;
  }
  if(!theForm.email.value) {
    alert('You need to supply your email.');
    theForm.email.focus();
    return false;
  }
  if(!theForm.password.value) {
    alert('You need to supply your password.');
    theForm.password.focus();
    return false;
  }
  if(!theForm.nickname.value) {
    alert('You need to supply your nickname.');
    theForm.nickname.focus();
    return false;
  }
  return true
}
</script>

Using 使用

<form action="register.php" onsubmit="return validate(this)">

You should duplicate (at least) the code that you use is Javascript in the PHP code for the insertion into your database. 您应该(至少)复制用于插入数据库的PHP代码中的Javascript(至少)。 In fact the code for the insertion should be tougher - Javascript stuff is to pick up the usual suspects (typos etc) and to give the user a better experience. 实际上,用于插入的代码应该更严格-Javascript的作用是拾取通常的可疑对象(打字错误等)并为用户提供更好的体验。

Just ensure that the Javascript code is less harsh than the PHP code. 只要确保Javascript代码不比PHP代码苛刻即可。

Agree with mplungjan. 同意mplungjan。

Can you show us the form html code? 您能告诉我们html代码吗? Maybe the submit input has a different id than 'submit'. 也许提交输入的ID与“提交”的ID不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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