2

私は、顧客を選択して、対応する顧客の詳細をデータベースから取得できるページを作成しようとしています。次のようになります。

<select id="select_customer">   
  <option value='1'>customer 1</option>
  <option value='1'>customer 2</option> 
</select>

public function getCustomerDetails($customerId) {
    if(isset($customerId)) {
        $customer = DB::getInstance()->query("select * from customers");
        foreach($customer->results() as $customer) {
            $str = "<li>{$customer->name}</li>";
            $str .= "<li>{$customer->name_contactperson}</li>";
            $str .= "<li>{$customer->email}</li>";
            $str .= "<li>{$customer->address} {$customer->house_number}</li>";
            $str .= "<li>{$customer->postalcode}</li>";
            $str .= "<li>{$customer->city}</li>";
            $str .= "<li>{$customer->country}</li>";
        }

        return $str;
    } 
    return false;
}

私が今やりたいことは、select_customerから値を取得し、これをajaxでgetCustomerDetailsメソッドに投稿し、ページをリロードせずに対応する詳細を取得することです。ajax と xAjax で動作させようとしましたが、動作させることができませんでした。

私はこれを試しました:

<?php include 'xajaxAIO.inc.php'; 
$xajax = new xajax(); 
$xajax->register(XAJAX_FUNCTION, 'getCustomers'); 
$xajax->processRequest(); ?> 
<input type="button" onclick="xajax_getCustomerDetails(1);" value="Click Me" />

私が試した他のことはこれでした:

<script>
document.getElementById('select_customer').addEventListener('change', function() {
    var $userId = this.value;
    $.ajax({
        type: "POST",
        url: "classes/invoice.php",
        data: "getCustomerDetails("+$userId+")"
    })
});
</script>

コンソールにエラー メッセージは表示されませんが、要求された関数が実行されないようです。

これを機能させる方法を教えてくれる人はいますか?

前もって感謝します!

4

2 に答える 2

1

$userId だけを送信してから、invoice.php ページで getCustomerDetails($userId) を呼び出すことをお勧めします。

$.ajax({
    type: "GET",
    url: "classes/invoice.php",
    data: $userId
  })
});

また

$.ajax({
    type: "GET",
    url: "classes/invoice.php&function=getCustomerDetails&userId="+$userId
    dataType: "json", //Dont need this if youre returning a string
    success: function(result) {
        alert(result);
    }
  })
});

次に、請求書ページで、次のように $_GET 変数を使用して関数を呼び出すことができます。

 $response = 'error;
 if($_GET['function'] == 'getCustomerDetails'){
      if(!empty($_GET['userId'])){
           $_GET['userId'] = 0;
       }
       $userID = $_GET['userId'];
       $response = getCustomerDetails($userID);
 }
die(json_encode($response)); //for array
die($response); //for a string
于 2016-01-08T15:55:27.517 に答える
0

xajax フレームワークを使用している場合... 新しい関数を登録する必要があります...

<?php include 'xajaxAIO.inc.php'; 
$xajax = new xajax(); 
$xajax->register(XAJAX_FUNCTION, 'getCustomers');
$xajax->register(XAJAX_FUNCTION, 'getCustomerDetails'); 
$xajax->processRequest(); ?> 
<input type="button" onclick="xajax_getCustomerDetails(1);" value="Click Me" />

<?php function getCustomerDetails($id){
    ///////////////////////////////////////////////
    ///// And use framework xajax to response /////
    ///////////////////////////////////////////////
}?>
于 2016-04-15T15:09:19.200 に答える