通过PHP / jQuery / Ajax检索多个值

时间:2017-04-04 23:11:02

标签: php ajax

我正在使用以下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);   

?>

但是我如何发回(并接受)一个额外的值,所以String Name和Integer Key?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我总是在ajax中执行此返回格式。

成功回应

// PHP

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

失败响应

// PHP

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

在将数据返回到ajax示例json_encode($result)时使用json编码,也不要忘记在ajax中添加dataType设置,以便它可以期待json格式的响应。

Ajax 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
        }
    }
});