2017-04-04 34 views
0

我使用下面的jQuery來爲 'Live搜索' 字段檢索值:通過PHP/jQuery的/阿賈克斯檢索多個值

$(document).ready(function(){ 
/* LIVE SEARCH CODE - START HERE*/ 
var UserID = ('<?php echo $_SESSION['UserID'] ?>'); 
$(document).ready(function(){ 

    $('.clLiveSearchAccount').on("keyup" , "[id*=txtAccountID]", "input", function(){ 
    /* Get input value on change */ 
    var inputVal = $(this).val(); 
    var ParentTransID = $(this).prev().val(); 
    alert(UserID); 
    var resultDropdown = $(this).siblings(".result"); 
    if(inputVal.length){ 
     $.get("Apps/Finance/incGetAccounts.php", {term: inputVal, usr: UserID }).done(function(data){ 
     // Display the returned data in browser 
     resultDropdown.html(data); 
     }); 
    } else{ 
     resultDropdown.empty(); 
    } 
    }); 

    // Set search input value on click of result item 
    $(document).on("click", ".result p", function(){ 
    $(this).parents(".clLiveSearchAccount").find('#txtAccountID').val($(this).text()); 
    $(this).parent(".result").empty(); 
    }); 
}); 

我使用這個PHP AJAX的處理程序:

<?php 
    /* ------------------------------------------------ */ 
    $link = mysqli_connect("xxx", "xxx", "xxx", "xxx"); 

    // Check connection 
    if($link === false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
    } 

    // Escape user inputs for security 
    $term = mysqli_real_escape_string($link, $_REQUEST['term']); 
    $user = mysqli_real_escape_string($link, $_REQUEST['usr']); 

    if(isset($term)){ 
    // Attempt select query execution 
    $sql = "SELECT * FROM tblAccounts WHERE Name LIKE '%" . $term . "%' AND UserID=" . $user; 
    if($result = mysqli_query($link, $sql)){ 
     if(mysqli_num_rows($result) > 0){ 
     while($row = mysqli_fetch_array($result)){ 
      echo "<p>" . $row['Name'] . "</p>"; 
     } 
     // Close result set 
     mysqli_free_result($result); 
     } else{ 
     echo "<p>No matches found</p>"; 
     } 
    } else{ 
     echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
    } 
    } 

    // close connection 
    mysqli_close($link); 

?> 

但是,我如何發回(並接受)一個額外的值,所以字符串名稱和整數鍵?

回答

0

看來你正在尋找發送JSON數據。把想要回顯的HTML放入一個變量中。

$html ="<h1>PHP is Awesome</h1>"; 
$myVariable = 3; 
echo json_encode(array('variable1' => $myVariable, 'html' => $html)); 

,你需要一個成功回調在JavaScript

success: function($data) { 
    var html = $data.html; 
    var myVar = $data.variable1; 

    // other stuff 
} 

查找一個教程PHP JSON W3Schools的始終是一個良好的開端 https://www.w3schools.com/js/js_json_php.asp

0

我總是這樣做在ajax中返回格式。

成功響應

// PHP

$result = [ 
    'success' => true, 
    'variable' => $myVariable, 
    'html' => $html, 
]; 

失敗響應

// PHP的數據返回到AJAX例如當

$result = [ 
    'success' => false, 
    'err_message' => 'Error message here!', 
], 

使用JSON編碼json_encode($result)和還不要忘記在你的ajax中添加dataType設置,以便它會期待json格式響應。

阿賈克斯FN

$.ajax({ 
    url: 'path to php file', 
    type: '' // specify the method request here i.e POST/GET 
    data: {} // data to be send 
    dataType: 'JSON', 
    success: function(res) { 
     if (res.success) { 
      ... 
     } else { 
      // you can put the error message in html by accessing res.err_message 
     } 
    } 
});