Javascript 将单击链接的ID传递给PHP代码,而不重新加载页面
我试图在不重新加载页面的情况下将单击链接的ID传递给PHP代码 目前,我正在使用以下代码在标记的href属性中传递ID:Javascript 将单击链接的ID传递给PHP代码,而不重新加载页面,javascript,php,html,mysql,Javascript,Php,Html,Mysql,我试图在不重新加载页面的情况下将单击链接的ID传递给PHP代码 目前,我正在使用以下代码在标记的href属性中传递ID: <?php $sql="select * from category"; $execute=mysql_query($sql); while($row=mysql_fetch_assoc($execute)){ ?> <li><a href="index.php?did=<?php echo
<?php
$sql="select * from category";
$execute=mysql_query($sql);
while($row=mysql_fetch_assoc($execute)){ ?>
<li><a href="index.php?did=<?php echo $row['id']; ?>" onclick="javascript:show()"><?php echo $row['category'] ; ?></a></li>
<?php
}
?>
</ul>
</div>
<div id="waxing" style="display:none">
<h2 id="cat_price" style="color:#f5c658">Price Category Waxing</h2>
<ul class="ul1">
<?php
function sub() {
if(isset($_GET['did']))
{
$id=$_GET['did'];
$sql="select * from midcat where cid=$id";
$execute=mysql_query($sql);
if(mysql_num_rows($execute)>0){
while($row=mysql_fetch_assoc($execute)){
?>
<li><a href="#" id="wax_normal" ><?php echo $row['midcat']; ?> </a></li>
<?php
}
}
}
}
sub();
?>
价格类别打蜡
是否可以在不重新加载页面的情况下执行相同的操作?如果不想刷新页面,则必须使用AJAX: 在javascript onclick事件上,传递id并用哈希标记替换href属性值。像这样:
<a href="#" onclick="javascript:show(<?php echo $row['id']; ?>)"><?php echo $row['category'] ; ?></a>
现在在show函数中发送一个ajax请求。然后使用响应。正如我提到的,您将希望使用
AJAX
获取其他列表。我将演示并记录。我之所以使用jQuery,是因为我熟悉它,并且发现它很容易使用。传统的javascript AJAX也同样适用:
page1.php(不管这个页面叫什么)
在
中添加jQuery库:
在同一页面上添加ajax:
<script>
// When the page finishes loading
$(document).ready(function() {
// When the user clicks any <li>, activate script
$("li").click(function() {
// Assign the value of the data attribute
var thisBtn = $(this).data('id');
// If an <li> doesn't have this attribute, stop
if(thisBtn == undefined)
return false;
// Start the ajax
$.ajax({
// Where to send request
url: 'ajax.index.php',
// What to send
data: { did: thisBtn },
// How to send
type: 'post',
// What to do when request succeeds
success: function(response) {
// Save the contents of the response into
// whatever has the id="list"
$("#list").html(response);
}
});
});
});
</script>
//当页面完成加载时
$(文档).ready(函数(){
//当用户单击任何时,激活脚本
$(“li”)。单击(函数(){
//指定数据属性的值
var thisBtn=$(this).data('id');
//如果 没有此属性,请停止
如果(thisBtn==未定义)
返回false;
//启动ajax
$.ajax({
//向何处发送请求
url:'ajax.index.php',
//发送什么
数据:{did:thisBtn},
//如何发送
键入:“post”,
//请求成功时要做什么
成功:功能(响应){
//将响应的内容保存到
//id=“list”中包含的内容
$(“#列表”).html(回复);
}
});
});
});
在希望列表显示在此页面上的位置放置一个放置点:
<!-- this is where ajax will drop the list on your page -->
<span id="list"></span>
创建一个新页面,并将
,函数,函数的执行放在此新页面上
ajax.index.php
<?php
function sub()
{
// In the ajax, I told it to use $_POST, so change to
// post instead of get
if(isset($_POST['did'])) {
$id=$_POST['did'];
// Don't do this anymore.............vvv
$sql="select * from midcat where cid=$id";
// Don't use mysql_ anymore
$execute=mysql_query($sql);
if(mysql_num_rows($execute)>0){
while($row=mysql_fetch_assoc($execute)){
?>
<li><a href="#" id="wax_normal" ><?php echo $row['midcat']; ?> </a></li>
<?php
}
}
}
}
?>
<ul class="ul1">
<?php sub().PHP_EOL; ?>
</ul>
您可以将其保存在变量中并检查是否为空。若要不重新加载页面,请使用AJAX
。我发现jQuery AJAX特别易于使用/实现。我不能具体支持jQuery,但是的,AJAX是实现这一点所需的工具。将整个函数子()
业务移动到一个新页面,并使用AJAX访问该新页面以填充新的
列表。另外,不要使用mysql\uu
尤其是如何使用它(直接注入sql语句)
<!-- this is where ajax will drop the list on your page -->
<span id="list"></span>
<?php
function sub()
{
// In the ajax, I told it to use $_POST, so change to
// post instead of get
if(isset($_POST['did'])) {
$id=$_POST['did'];
// Don't do this anymore.............vvv
$sql="select * from midcat where cid=$id";
// Don't use mysql_ anymore
$execute=mysql_query($sql);
if(mysql_num_rows($execute)>0){
while($row=mysql_fetch_assoc($execute)){
?>
<li><a href="#" id="wax_normal" ><?php echo $row['midcat']; ?> </a></li>
<?php
}
}
}
}
?>
<ul class="ul1">
<?php sub().PHP_EOL; ?>
</ul>