2017-05-26 208 views
-3

我在阿賈克斯新我感到困惑becouse我想我的AJAX文件沒有將數據發送到PHP文件或PHP是沒有得到它,IDK,請幫助我Ajax不是將數據發送到PHP

這的形式是

<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> 

這是的.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"); 
     } 
    }); 
}); 

});

這是.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')");?> 
+1

您需要更好地解釋問題,什麼是不工作,爲什麼你認爲它不工作,你已經試圖自己解決它,等等。 「idk請幫助我」不是一個問題。如果我是一個博彩人,我會冒險猜測您的頁面在AJAX調用完成之前刷新,因爲您正在使用「submit」按鈕而不會阻止其實際提交。 (即'preventDefault()') – Santi

+0

'$ username $ _POST [「username」]; $ password $ _POST [「username」];'一件事,是翻倍的。 –

+0

'$ username $ _POST [「username」]; $ password $ _POST [「username」];''=''''''''''''''' – Juned

回答

0
$(document).ready(function(){ 
$("#submit").click(function(event) { 
    event.preventDefault(); 

    var inputEmail = $("#email").val(); 
    var inputUsername = $("#username").val(); 
    var inputPassword = $("#password").val(); 

    $.ajax({ 
      type: "POST", 
      url: "register.php", 
      data: ({ email: inputEmail, password: inputPassword, username: inputUsername}), 
      success: function(data){ 
       var obj = jQuery.parseJSON(data); 

       alert("Success " + obj.username + " " + obj.password + " "+ obj.email); 
      } 
    }); 


}); 

});

在這裏,在.js文件中我把在頂部.click(function(event) { event.preventDefault(); }

preventDefault(); 

這個功能阻止該網頁realoding當您按下提交按鈕

data: ({ email: inputEmail, password: inputPassword, username: inputUsername}) 

在這裏,我將數據發送data: ({nameOfTheVarieableYouWantToReadWithPHP: nameOfTheVariableFromJs})

這裏是.php文件

require_once("database.php"); //require the connection to dabase 

$email = protect($_POST['email']);  //This will read the variables 
$username = protect($_POST['username']); //sent from the .js file 
$password = protect($_POST['password']); // 

$result = array(); //This variable will be sent back to .js file 
//check if the variables are emtpy 
if(!empty($email) && !empty($username) && !empty($password)){ 
    //db_query is my function from database.php but you can use mysqli_query($connectionVariable, $sqlString); 
    db_query("INSERT INTO users (email, username, password) VALUES ('$email','$username','$password')"); //Here insert data to database 
//we will set array variables 
$result['username'] = $username; //Here we set the username variable fron the array to username variable from js 
$result['password'] = $password; // the password same as the username 
$result['email'] = $email; //the email same as the username and password 
}else{ // if the variables are empty set the array to this string 
    $result = "bad"; 
} 
echo json_encode($result); //transform the result variable to json 

在。js文件

 success: function(data){ 
      var obj = jQuery.parseJSON(data); //create a variable and parse the json from the php file 
      //You can set the variables get from the json 
      var usernameFromPhp = obj.username; 
      var passwordFromPhp = obj.password; 
      var emailFromPhp = obj.email; 

      alert("Success " + usernameFromPhp + " " + passwordFromPhp + " "+ emailFromPhp);// 
     } 
+0

發表評論如果有什麼不明確的,我回答最好的我可以 – Bidijoe45

+0

這對我有效 – Bidijoe45

2

首先:

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

應該是:

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


data: "email="+email+"&username="+username+"&password="+password 

應該是:

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

而且

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

應該是:

$username = $_POST["username"]; 
$password = $_POST["password"]; 
+1

'$ username = $ _POST [「username」]; $ password = $ _POST [「username」];'所以密碼是用戶名的POST數組;我懷疑這會讓他們失望。 –

0

1:而不是使用submit input點擊事件,你可以使用form submit event

$("#register-form").on('submit',function(){ 

,並在您使用一個提交確定你需要防止從默認重新加載頁面..我認爲你的問題是這一點..所以你需要通過使用e.preventDefault();阻止形式,你可以使用我像

$("#register-form").on('submit',function(e){ 
    e.preventDefault(); 
    // rest of code here 
0

如果將t發送JSON格式的數據,如:

var data = { "email": email, "username": username, "password": password }; 

這樣傳遞數據的VAR在數據的Ajax功能!