Ajax不会在数据库中提交和存储信息?

时间:2017-03-13 22:15:03

标签: php html ajax

创建一个register.html表单,用户提示输入名称,电子邮件和密码,然后点击提交,触发php脚本检查电子邮件输入是否已被使用,如果尚未使用,它将用户数据放在表格“用户 - 表格”中的相应字段中。在mysql上。

我的问题是AJAX没有提交表单。 javascript表示"填写空字段"罚款。我不知道自己做错了什么。

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Reg Form</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  </head>
  <body>
    <div class="header">
      <div>Register form</div>
    </div>
    <form action="">
      <p> Enter name, email and password</p>
      <label>Name :</label>
      <input id="name" type="text">
      <label>Email :</label>
      <input id="email" type="text">
      <label>Password :</label>
      <input id="password" type="password">
      <input id="submit" type="button" value="Submit">
    </form>

    //Internal Javascript
    <script>
      $(document).ready(function() {
        $("#submit").click(function() {
          var name = $("#name").val();
          var email = $("#email").val();
          var password = $("#password").val();
          //success message when information is stored in database.
          var dataString = 'name1=' + name + '&email1=' + email + '&password1=' + password;
          if (name == '' || email == '' || password == '') {
            alert("Fill in empty fields");
          } else {
            //submits form
            $.ajax({
              type: "POST",
              url: "connect.php",
              data: dataString,
              cache: false,
              success: function(result) {
                alert(result);
              }
            });
          }
          return false;
        });
      });
    </script>

  </body>
</html>

AJAX connect.php:

<?php
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server..
$db = mysql_select_db("mydatabase", $connection); // Selecting Database
//Fetching Values from URL
$name2=$_POST['name1'];
$email2=$_POST['email1'];
$password2=$_POST['password1'];

//Insert query
$query = mysql_query("insert into users-form(name, email, password) values ('$name2', '$email2', '$password2')");
echo "Submitted!";
mysql_close($connection); // Connection Closed
?>

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Reg Form</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
    <script src="js/theme.init.js"></script>
        <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    </head>
    <body>

    <div class="header">            
    <div>Register form</div>        
    </div>      

    <form action="" id="form">
    <p> Enter name, email and password</p>

    <label>Name :</label>
    <input id="name" name="name1" type="text">

    <label>Email :</label>
    <input id="email" name="email1" type="text" required>

    <label>Password :</label>
    <input id="password" name="password1" type="password" required>

    <input id="submit" type="button" value="Submit">
    </form>

    //Internal Javascript

                                    <script type="text/javascript">

                                    $(document).ready(function (e){
                                    $("#form").on('submit',(function(e){

                                        e.preventDefault();

                                            $.ajax({
                                            url: "connect.php ",
                                            type: "POST",
                                            data:  new FormData(this),
                                            contentType: false,
                                            cache: false,
                                            processData:false,
                                            success: function(result) {
                                            alert(result);
                                            }           

                                            });
                                            return false

                                    }));


                                    });
                        </script>
</body>
</html>

答案 1 :(得分:0)

您的javascript代码很好,问题是[{1}}已弃用,而您可以使用mysql,因此您的连接可能是这样的:

mysqli