2017-04-03 13 views
0

我無法從PHP腳本獲取所需的所有信息,以響應從網頁發出的AJAX請求。如何從mysql中獲取值,返回到前端並在url中使用

我有一個前端形式:

<form method="post" id="register-form"> 
    <input class="form-control" id="firstname" name="first name" placeholder="Fornavn" type="text" /> 
    <button type="submit" class="btn btn-success" name="btn-save" id="btn-submit"> 
     Next page &nbsp; <i class="fa fa-arrow-right"></i> 
    </button> 

它使用AJAX來發布數據:

function submitForm(){ 
    $.ajax({ 
     type : 'POST', 
     async: false, // NEW 
     url : 'register.php', 
     data : $("#register-form").serialize(), 
     dataType: 'json', 
     success : function(data){ 
      if (data.status==='success') { 
       $("#btn-submit").html('<span class="fa fa-spinner fa-spin"></span> &nbsp; Saving ...'); 
       $('#btn-submit').attr('disabled', 'disabled'); 
       $(".reg-form").fadeOut(500); 
       $(".ChooseProductForm").fadeIn(500); 
      } 

     }); 
     return false; 
    } 

這後一個成功的響應結束了顯示兩個鏈接按鈕:

<a href="goHome.php?userID=XX" class="btn btn-buy-now" role="button">Go home</a> 
<a href="goAway.php?userID=XX" class="btn btn-buy-now" role="button">Go Away</a> 

在鏈接href值中,XX是佔位符字符$LastInsertedID在PHP中生成的值。

我的PHP文件:

<?php 
$response = array(); 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $user_firstname   = trim_inputs($_POST['firstname']); 
    $user_joining_date  = date('Y-m-d H:i:s'); 
    $response['status'] = 'success'; 
    $SaveData = "INSERT INTO Users (firstname, joined) 
       VALUES ('".$user_firstname."', '".$user_joining_date."')"; 

    if (mysqli_query($link, $SaveData)) { 
     $LastInsertedID = mysqli_insert_id($link); 
    } else { 
     $response['status'] = 'error'; 
     //echo "Error: " . $SaveData . "<br>" . mysqli_error($link); 
    } 
    echo json_encode($response); 
} 
?> 

一切正常,就像它應該,但不能瞭解如何獲得$LastInsertedID值回前端的兩個按鈕。

我知道有一個數組用於$ response,但是如何獲取數值。 還顯示一條消息,如:

$response['message'] = 'Welcome user'; 

我可以出去,但如果我再添加URL或< A HREF ...標籤到該消息時,將不會顯示。

感謝您閱讀了這個問題,希望這是可以理解的,也許有人可以幫幫忙:-)

提前感謝!

+3

如何生成的兩個按鈕?您也可以使用SQL進行注入。 – chris85

+0

將ID放入您的回覆中。在你的成功回調裏面(在submitForm函數中)從響應中獲取id,你有兩個選擇:1)將它保存在一個cookie中,然後在下一頁讀取該cookie。 2。)把id放在url中,然後當下一頁加載時,從url中獲取這個id – victor

+1

**警告**:當使用'mysqli'時,你應該使用[參數化查詢](http://php.net/manual /en/mysqli.quickstart.prepared-statements.php)和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **不要**將'$ _POST','$ _GET'或**任何**用戶數據直接放入查詢中,如果有人試圖利用您的錯誤,這可能會非常有害。 – tadman

回答

1

發送新的ID後面:

if (mysqli_query($link, $SaveData)) { 
    $LastInsertedID = mysqli_insert_id($link); 
    $response['userId'] = $LastInsertedID; 
} else { 
    $response['status'] = 'error'; 
} 

使用該ID:

success : function(data) 
{ 
    if (data.status==='success') { 
     $("#btn-submit").html('<span class="fa fa-spinner fa-spin"></span> &nbsp; Saving ...'); 
     $('#btn-submit').attr('disabled', 'disabled'); 
     $(".reg-form").fadeOut(500); 
     $(".ChooseProductForm").fadeIn(500); 
     $("#id-of-go-away-button").attr("href", "goAway.php?userId=" + data.userId); 
     $("#id-of-go-home-button").attr("href", "goHome.php?userId=" + data.userId); 
    } 
} 
+0

非常感謝您的解決方案。現在我明白我應該做什麼了! – KommerSnart

1

小的變化給ja​​vascript。我移動了代碼以顯示微調器,並在ajax調用之前告訴用戶您要保存的內容。 然後我添加了代碼以從ajax調用返回的數據中獲得新的ID,並將其放入字段中。

JavaScript代碼:

function submitForm(){ 
    $("#btn-submit").html('<span class="fa fa-spinner fa-spin"></span> &nbsp; Saving...'); 
    $('#btn-submit').attr('disabled', 'disabled'); 
    $.ajax(
     { 
      type : 'POST', 
      async: false, // NEW 
      url : 'register.php', 
      data : $("#register-form").serialize(), 
      dataType: 'json', 
      success : function(data) { 
       if (data.status==='success') { 
        $("#btn-submit").html('Saved.'); 
        $(".reg-form").fadeOut(500); 
        $(".ChooseProductForm").fadeIn(500); 
        var new_id = data.new_id; 
        $("#field to hold the new id").val(new_id); 
       } 
      } 
     } 
    ); 
    return false; 
} 

在服務器上,我們只需要添加新的ID將數據返回給調用者。

PHP代碼:

<?php 
    $response = array(); 
    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
     $user_firstname = trim_inputs($_POST['firstname']); 
     $user_joining_date = date('Y-m-d H:i:s'); 
     $response['status'] = 'success'; 
     $response['error_message'] = ""; 
     $SaveData = "INSERT INTO Users (firstname, joined) VALUES ('".$user_firstname."', '".$user_joining_date."')"; 

     if (mysqli_query($link, $SaveData)) { 
      $LastInsertedID = mysqli_insert_id($link); 
      $response['new_id'] = $LastInsertedID; 
      $response['message'] = "Welcome new user!"; 
     } else { 
      $response['status'] = 'error'; 
      $response['error_message'] = $SaveData . "<br>" . mysqli_error($link); 
     } 
     echo json_encode($response); 
    } 
?> 
相關問題