$ID = $_GET['ID'];
$results = $mysqli->query("SELECT * FROM PresidentialCandidate WHERE ID='$ID'");
if( $results->num_rows > 0 )
{
$row = mysqli_fetch_array($results,MYSQLI_ASSOC);
//Instead of just echoing out the ID, you need to build the result/data that you want in the div right here. The success function of the AJAX call will append whatever you echo out here
echo $row['ID'];
}
不知道怎么了。 这是一个基于我原始问题上发布的解决方案的问题:使用 href onclick 在不重新加载页面的情况下更新div? 如何定义 ID 变量?
您的代码可能会导致 2 个不同的问题。但是您以相同的方式命名了变量。
1.( 检查是否设置了 GET 变量,否则使用默认值初始化它们。但在您的情况下,最好检查它之前是否设置,如果未设置,则不要执行查询。
$ID = (int)$_GET['ID'];
if(isset($_GET['ID']) {
$results = $mysqli->query("SELECT * FROM PresidentialCandidate WHERE ID='$ID'");
if( $results->num_rows > 0 ) {
$row = mysqli_fetch_array($results,MYSQLI_ASSOC);
echo $row['ID'];
}
}
2.(如果您的数据库中没有称为ID
的确切字段,那么您会遇到下一个错误。您应该在之前检查它们。
最后一件事是你以一种错误且非常不安全的方式使用 mysqli。您可以将 GET 变量直接传送到查询,而无需使用预准备语句或检查它是否为 int。
https://secure.php.net/manual/en/mysqli.prepare.php
你真的应该看看这一点。
您需要
在<a></a>
中设置像data-attrb
这样的属性(不需要同名(,并在<script></script>
中使用此属性。您可以在data-attrb
中传递您的值,这将存储在var ID=$(this).attr('data-attrb');
中<script></script>
。为 <a></a>
声明一个类名,使用此类名调用该load()
函数。我已经检查了此代码,它正在工作。
<a href="#" class="asd" data-attrb='1277'> Two </a>
<a href="#" class="asd" data-attrb='1235'> Three </a>
<div id='myStyle'></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".asd").click(function(){
var ID=$(this).attr('data-attrb');
$("#myStyle").load("templatecode.php?id="+ID);
});
});
</script>
模板代码.php (此页面名称也用于<script></script>
标记中,因此如果您要在此处更改页面名称。也请在此处更改页面名称。
<?php
$ID = $_GET['id'];
$results = $mysqli->query("SELECT * FROM PresidentialCandidate WHERE ID='$ID'");
if( $results->num_rows > 0 )
{
$row = mysqli_fetch_array($results,MYSQLI_ASSOC);
//Instead of just echoing out the ID, you need to build the result/data that you want in the div right here. The success function of the AJAX call will append whatever you echo out here
echo $row['ID'];
}
?>