我對此主題進行了一些研究,但我無法運行此 id is not send to page。
串列資料.php
<td class="text-center" style="min-width:130px;">
<button style="width:100%" class="btn btn-primary detail-customer" data-id="<?php echo $m->id; ?>">Bilgi</button>
</td>
ajax.php
$(document).on("click", ".detail-customer", function() {
var id = $(this).attr("data-id");
$.ajax({
method: "POST",
url: "<?php echo base_url('customers/info'); ?>",
data: "id=" id
})
.done(function(data) {
window.location = "customers/info";
})
})
控制器:
public function info()
{
$data['page'] = "customer_information";
$data['userdata'] = $this->userdata;
$id= trim($_POST['id']);
print $id;
//$this->template->views('customers/info', $data);
}
uj5u.com熱心網友回復:
您是否將 ajax 檔案命名為Ajax.php?那是一個PHP檔案嗎?它絕對應該是一個js檔案。
根據我從 Jquery 和 Ajax 中了解到的情況,urlkey 獲取 PHP 檔案的位置,并且datakey 應該作為物件傳遞。以這兩個檔案為例:
Ajax.js
$(document).on("click", ".detail-customer", function() {
var id = $(this).attr("data-id");
$.ajax({
type: "POST",
url: "/absolute_path/to/php_file.php",
data: {my_id:id, my_custom_key: "my_custom_value"},
dataType: "JSON",
success: function (response) {
console.log(response);
},
error: function(response){
console.log(response);
}
})
.done(function(data) {
window.location = "customers/info";
})
})
php_file.php
<?php
$id = $POST["my_id"];
$customValue = $POST["my_custom_value"];
$response = ["message"=> "We took your id which is $id and your custom value which is $customValue"];
echo json_encode($response);
?>
這是如何使用 ajax 的一個非常基本的示例。
uj5u.com熱心網友回復:
請嘗試一下
$(document).on("click", ".detail-customer", function() {
var id = $(this).data("id");
$.ajax({
method: "POST",
url: "<?php echo base_url('customers/info'); ?>",
data: {"id":id},
Content-Type:"json"
});
.done(function(data) {
window.location = "customers/info";
})
});
“如果您的代碼是正確的,我們不會閱讀您完整的控制器代碼,那么請先將資料轉換為 json,然后回傳。”
public function info()
{
$data['page'] = "customer_information";
$data['userdata'] = $this->userdata;
$id= trim($_POST['id']);
echo json_encode($id);
//$this->template->views('customers/info', $data);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418576.html
標籤:
上一篇:引導日期選擇器固定在頁面頂部
