简体   繁体   English

表单不会将数据保存到 mySQL 表中

[英]Form doesn't save data into mySQL tables

I'm using mySQL, javascript and php.我正在使用 mySQL、javascript 和 php。 I have a form in my website to register new users into it.我在我的网站上有一个表格来注册新用户。 It's connected to a users table in my mySQL where it saves the users data.它连接到我的 mySQL 中的用户表,用于保存用户数据。

The thing is I can't apply the javascript constraints to the field forms unless I have event.preventDefault();问题是我不能将 javascript 约束应用于字段表单,除非我有event.preventDefault(); , it will just connect to my php page no matter what and save the data into my table. ,无论如何它都会连接到我的php页面并将数据保存到我的表中。
But if I use the event.preventDefault();但是如果我使用event.preventDefault(); it will apply the constraints but when I fill it correctly and click register it won't save anything, just creates an empty user in mysql table.它将应用约束,但是当我正确填写并单击注册时,它不会保存任何内容,只会在 mysql 表中创建一个空用户。

How can I solve this?我该如何解决这个问题?

var constraints = {
  email: {
    email: true,
    presence: true,
  },
  Telefone: {
    presence:true,
    format: {
      pattern: "[+0-9]+",
      message: "apenas pode conter números [0-9]"
    }
  },
  pwd: {
    presence: true,
    length: {
      minimum: 5
    }
  },
  cpwd: {
    presence: true,
    equality: {
      attribute: "pwd",
      message: "as passwords não coincidem"
    }
  }
}


  $("#validationForm").submit(function(event){

     event.preventDefault();

  $("#error").html("");

  console.log(event);

  var errors = validate($("#validationForm"), constraints);

  if (errors) {
    $("#submited").html("");
    for (var key in errors) {
      $("#error").append(errors[key] + "<br />");
      $("#" + key).css("border","1px red solid");
    }       
  }else {
    // $("#submited").html("Form Submited");

    // redirect to php
     window.location.href="registar.php";
  }
});

That's because you are manually redirecting to your PHP page.那是因为您正在手动重定向到您的 PHP 页面。

Instead of this line:而不是这一行:

 window.location.href="registar.php";

You should rather你应该

 document.getElementById("form_to_submit").submit();

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

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