Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

javascript - Ajax is not sending data to 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')");?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...