Mr *_*ide 6 php ajax jquery append
这是从数据库r_job表中获取的lotal列表..当我单击查看详细信息按钮时,它必须通过ajax将作业值带到控制器页面,然后我将获取作业信息,并且在响应中我必须发送作业相关表feillds并且必须在此页面上显示.
这是我的动态列表:
$Jobquery = $conn->query("SELECT * FROM r_job ");
while($JobResults = $Jobquery->fetch_assoc()){
<tr>
<td id="hiringevent"><?php echo $JobResults['hiringevent']; ?></td>
<td id="JobId"><?php echo $JobResults['id_job']; ?></td>
<td><button id="ViewDetails" class="btn btn-primary text-center">View Details</button></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
这是我的ajax和jquery调用:
$("#ViewDetails").click(function() {
$.ajax({
url: "job-controller.php",
method: "POST",
data: {'action':'viewjob','JobId' : + $('#JobId').html()},
dataType: "json",
success: function (response) {
$("#showMessage").html(response['message']);
},
error: function (request, status, error) {
$("#showMessage").html("OOPS! Something Went Wrong Please Try After Sometime!");
}
});
return false;
});
Run Code Online (Sandbox Code Playgroud)
最后我的控制器页面:
if($_POST['action']=='viewjob'){
$jobSearch= $conn->query("SELECT * From r_job WHERE id_job='".$_POST['JobId']."'") or die(mysql_error());
$ViewJob=$jobSearch->fetch_assoc();
$hiringevent = $ViewJob['hiringevent'];
$jobname = $ViewJob['jobname'];
$jobdescription = $ViewJob['jobdescription'];
$cutoff = $ViewJob['cutoff'];
$joblocation = $ViewJob['joblocation'];
$interviewlocation = $ViewJob['interviewlocation'];
$jobexperience = $ViewJob['jobexperience'];
$response['message'] = "Show Job Information";
$response['success'] = true;
}else{
$response['message'] = "OOPS! Something Went Wrong Please Try After Sometime!";
$response['success'] = false;
}
echo json_encode($response);
exit;
}
Run Code Online (Sandbox Code Playgroud)
我当前的问题是,当我单击查看详细信息时,只有第一个查看详细信息按钮仍然没有响
请注意,每个页面应该有一个 id。您需要做的就是创建一个 js 函数并在单击按钮时调用它,并将 JobId 作为参数发送。当你处于循环中时效果很好。
超文本标记语言
$Jobquery = $conn->query("SELECT * FROM r_job ");
while($JobResults = $Jobquery->fetch_assoc()){
<tr>
<td id="hiringevent"><?php echo $JobResults['hiringevent']; ?></td>
<td id="JobId"><?php echo $JobResults['id_job']; ?></td>
<td><button class="btn btn-primary text-center" onClick="getDetails(<?php echo $JobResults['id_job']; ?>)">View Details</button></td>
</tr>
}
Run Code Online (Sandbox Code Playgroud)
JS
function getDetails(jobID){
// ajax Call Here
console.log(jobID)// you can see JobID Here.
// now set the values to by using id
$("#ViewJobId").val(response['jobData']['jobId']);
/* val is used to set aswell as get the values */
/*in case of td you have to use text insted of val*/
$("#ViewJobId").text(response['jobData']['jobId']);
}
Run Code Online (Sandbox Code Playgroud)