doujiang3997 2017-05-26 22:15
浏览 39
已采纳

Ajax不向PHP发送数据

I'm new at ajax and i am confused becouse i think my ajax file is not sending data to php file or php is not getting it, IDK, Help me please

This is the form

<form id="register-form" method="post" role="form" style="display: none;">
                                            <div class="form-group">
                                                <input type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="Username" value="">
                                            </div>
                                            <div class="form-group">
                                                <input type="text" name="email" id="email" tabindex="1" class="form-control" placeholder="Email Address" value="">
                                            </div>
                                            <div class="form-group">
                                                <input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="Password">
                                            </div>
                                            <div class="form-group">
                                                <input type="password" name="confirm-password" id="confirm-password" tabindex="2" class="form-control" placeholder="Confirm Password">
                                            </div>
                                            <div class="form-group">
                                                <div class="row">
                                                    <div class="col-sm-6 col-sm-offset-3">
                                                        <input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register Now">
                                                    </div>
                                                </div>
                                            </div>
                                        </form>

This is the .js

$(document).ready(function(){
$("#register-submit").click(function(){
    var email = $("#email").val();
    var username = $("username").val();
    var password = $("password").val();

    $.ajax({
        type: "POST",
        url: "register.php",
        data:  "email="+email+"&username="+username+"&password="+password,

        success:function(data){
           alert("succes");
        }
    });
});

});

This is the .php

<?php
require_once("functions.php");

    $email = $_POST["email"];
    $username $_POST["username"];
    $password $_POST["username"];
    mysqli_query($connection, "INSERT INTO users(email, username, password) VALUES('$email', '$username', '$password')");?>
  • 写回答

4条回答 默认 最新

  • dongzhuzhou4504 2017-05-26 22:20
    关注

    First of all:

    var username = $("username").val();
    var password = $("password").val();
    

    Should be:

    var username = $("#username").val();
    var password = $("#password").val();
    
    
    data:  "email="+email+"&username="+username+"&password="+password
    

    Should be:

    data:  {email: email, "username": username, password: password}
    

    And

    $username $_POST["username"];
    $password $_POST["username"];
    

    Should be:

    $username = $_POST["username"];
    $password = $_POST["password"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题