将带 href 的 ID 发送到带 jquery 的 php 页面
send ID with href to php page with jquery
所以,我有一个 CRUD 和一个要修改的按钮,它应该重定向到另一个带有该人 ID 的页面,我尝试用 jquery 这样做
fetchList()
function fetchList() {
$.ajax({
url: 'lista.php',
type: 'GET',
success: function (response) {
let task = JSON.parse(response);
let template = '';
task.forEach(task => {
template +=
`<tr pacienteId="${task.id_pac}">
<td>${task.apellido} </td>
<td>${task.nombre}</td>
<td>${task.dni}</td>
<td>${task.fecha_nacimiento}</td>
<td>${task.fecha_ingreso}</td>
<td>${task.obra_social}</td
// <td <span class="badge pacActivo">Activo</span> </td>
<td>
<div class="btn-group btn-group-toggle d-flex justify-content-center">
<a href="modificar.php" id="modificar" class="btn btn-sm butMod"></a>
<i class="fas fa-edit"></i>
<button class="btn btn-sm butDel darBaja">
<i class="fas fa-trash-alt"></i>
</button>
</div>
</td>
</tr>`
});
$("#listadoPacientes").html(template);
}
});
}
这是修改
$(document).on('click', '#modificar', function (e) {
var href = $(this).attr('href');
let element = $(this)[0].parentElement.parentElement.parentElement
let id = $(element).attr('pacienteId')
// Save information
// Check if any ID is aviable
if (id) {
// Save the url and perform a page load
var direccion = href + '&id=' + id;
window.open(direccion);
console.log(id)
} else {
// Error handling
alert('algo salio mal')
}
})
但我收到此错误的问题是:
Undefined index: id in
C:\xampp\htdocs\sistema\modulos\Pacientes\modificar.php
其中我有来自 jquery 的 ID 变量
- 解决方法:
您将
href
属性附加到 jQuery 中,仅使用“&”。你需要一个“?”而不是“&”。
- 解决方案:在 jQuery HTML 构建过程
href
-tag. 后面放置一个 GET 参数
简而言之,您正在生成一个 URL,例如:
modificar.php&id=0
正确的是
modificar.php**?**id=0
示例:
if (id) {
// Save the url and perform a page load
var direccion = href + '?id=' + id; // changed the & to an ?
window.open(direccion);
console.log(id)
} else {
// Error handling
alert('algo salio mal')
}
所以,我有一个 CRUD 和一个要修改的按钮,它应该重定向到另一个带有该人 ID 的页面,我尝试用 jquery 这样做
fetchList()
function fetchList() {
$.ajax({
url: 'lista.php',
type: 'GET',
success: function (response) {
let task = JSON.parse(response);
let template = '';
task.forEach(task => {
template +=
`<tr pacienteId="${task.id_pac}">
<td>${task.apellido} </td>
<td>${task.nombre}</td>
<td>${task.dni}</td>
<td>${task.fecha_nacimiento}</td>
<td>${task.fecha_ingreso}</td>
<td>${task.obra_social}</td
// <td <span class="badge pacActivo">Activo</span> </td>
<td>
<div class="btn-group btn-group-toggle d-flex justify-content-center">
<a href="modificar.php" id="modificar" class="btn btn-sm butMod"></a>
<i class="fas fa-edit"></i>
<button class="btn btn-sm butDel darBaja">
<i class="fas fa-trash-alt"></i>
</button>
</div>
</td>
</tr>`
});
$("#listadoPacientes").html(template);
}
});
}
这是修改
$(document).on('click', '#modificar', function (e) {
var href = $(this).attr('href');
let element = $(this)[0].parentElement.parentElement.parentElement
let id = $(element).attr('pacienteId')
// Save information
// Check if any ID is aviable
if (id) {
// Save the url and perform a page load
var direccion = href + '&id=' + id;
window.open(direccion);
console.log(id)
} else {
// Error handling
alert('algo salio mal')
}
})
但我收到此错误的问题是:
Undefined index: id in C:\xampp\htdocs\sistema\modulos\Pacientes\modificar.php
其中我有来自 jquery 的 ID 变量
- 解决方法:
您将
href
属性附加到 jQuery 中,仅使用“&”。你需要一个“?”而不是“&”。 - 解决方案:在 jQuery HTML 构建过程
href
-tag. 后面放置一个 GET 参数
简而言之,您正在生成一个 URL,例如:
modificar.php&id=0
正确的是
modificar.php**?**id=0
示例:
if (id) {
// Save the url and perform a page load
var direccion = href + '?id=' + id; // changed the & to an ?
window.open(direccion);
console.log(id)
} else {
// Error handling
alert('algo salio mal')
}