AJAX加载到另一个具有变量的div



嘿,我正试图用传递的变量加载另一个div。我在div#main中,我想要一个点击#modifybtn按钮后传递btn值,并用另一个div#modify加载它,并处理一些查询。我对jQuery和ajax没有兴趣,我在网上搜索,但无法找到解决方案。请检查相关代码并告诉我这个问题。

这是div#main#modifytn按钮和div#modify

<div id=main>
<button id="modifybtn" value="some_value" >Modify</button>
</div>
<div id="modify">
<?php 
$resultmodi=$conn->query("SELECT * FROM vacancy WHERE vc_id='{$_GET['id']}' LIMIT 1 ");
?>
</div>

内部的dashcompany.php

<div class="contentcm" >
//contentcm.php page load content here
</div>

这是我的jQuery,点击按钮后显示警报,但没有重定向到#modify-div

$(document).ready(function(){
$('.contentcm').load('contentcm.php #main');
$('a').click(function(){  //main and modify division in contentcm.php 
var clickedLink1 = $(this).attr('id');
$('.contentcm').load('contentcm.php #' + clickedLink1);
});
});
$(document).on("click", '#modifybtn', function(event) { 
var id = $(this).val();
alert(id);
event.preventDefault(); 
$.ajax({
url: 'dashcompany.php',
type: 'get',
data: {'id' : id},
success: function(response) {
$('#modify').html(response); 
}
});
});

这将是初始.html文件

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src = "FILE_NAME.js"></script>
</head>
<body>
<div id=main>
<button id="modifybtn" value="some_value" >Modify</button>
<div id="modify"></div>
</div>
</body>
</html>

FILE_NAME.js中的代码应该像一样

$(document).on("click", '#modifybtn', function(event) { 
var id = $(this).val();
alert(id);
event.preventDefault(); 
$.ajax({
url: 'dashcompany.php',
type: 'get',
data: {'id' : id},
success: function(response) {
$('#modify').html(response); 
}
});

您的js文件将从dashcompany.php加载数据,并在initial.html文件中的#modify中加载

dashcompany.php

<?php
include_once('connection.php');
$id = $_GET['id'];
$resultmodi=$conn->query("SELECT * FROM vacancy WHERE vc_id='$id' LIMIT 1 ");
$row = $resultmodi->fetch_assoc();
echo "Name: " . $row["name"];
''' print data whatever you need '''
$conn->close();
?>

原因:可能是您忘记在dashcompany.php文件中打印数据,这就是为什么您从ajax请求中得到空白响应的原因。

不要忘记将#modify-div包含在存在#main#modify-dev的同一个html文件中

您可以使用如下代码,我建议您使用post方法

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<button type='button' id="modifybtn" class='btn btn-info' value="some_value">click here</button>

<div class="" id="modify"></div>
<script type="text/javascript">
$(document).on("click", '#modifybtn', function(event) {
var  id = $(this).val();
//alert(id);
$.ajax({
url: 'dashcompany.php',
type: 'post',
data: {'id' : id},
dataType: 'json',
success: function(response) {
//now you can call with column name of data table
$('#modify').html("<P>"+response.column_one_name+"</p><br><P>"+response.column_two_name+"</p>");
}
});
});
</script>

你的dashcompany.php页面应该是这样的,

<?php 
// $localhost = "127.0.0.1";
// $username = "root";
// $password = "";
// $dbname = "db_304";
// 
// $connect = new mysqli($localhost, $username, $password, $dbname);
// // check connection
// if ($connect->connect_error) {
//     die("Connection Failed : " . $connect->connect_error);
// }
$id = $_POST['id'];

$sql = "SELECT * FROM vacancy WHERE vc_id = '$id' LIMIT 1 ";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_array();
} // if num_rows
$connect->close();
echo json_encode($row);

希望这对你有帮助。

最新更新