我一直试图制作一个页面,我可以选择一个客户,并从数据库中获取相应的客户详细信息。它看起来应该像这样:
<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>
我没有在控制台中收到任何错误消息,但似乎请求的功能未执行。
谁能告诉我如何使它起作用?
提前致谢!
我建议仅发送$ 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句