关于PHP用jQuery的ajax传参问题,请各位帮帮忙
我想实现的是下图这个功能
目前树结果已经用PHP生成完成(listhou.php),怎么才能点这个树的A标签不跳转页面传参到另一个PHP页面(listhou2.php)并取回结果呢?
HTML部分代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>测试</title><link rel="stylesheet" type="text/css" href="style/index.css" /><script type="text/javascript" src="js/jquery-1.11.3.min.js"></script><script type="text/javascript">$(function(){ $(".listshow ul").load("listhou.php");});</script></head><body><div class="listshow" ><ul></ul><a href="222">123</a><a href="333">321</a></div><div class="list2"> </div></body></html>
listhou.php代码
<?phpheader("Content-Type:text/html;Charset=utf-8");$host='localhost';$user='root';$password='63698000xw';$database='test';$conn=mysqli_connect($host, $user, $password, $database) or die('DataBase connected error<br/>'.mysqli_connect_error($conn));$query="SELECT * FROM company";$result=mysqli_query($conn, $query); while ($row=mysqli_fetch_assoc($result)) { echo '<li>'; echo '<h5 id="row-c-name">'.$row['c_name'].'</h5>'; echo '<ul>'; $query1="SELECT * FROM department"; $result1=mysqli_query($conn, $query1); while ($row1=mysqli_fetch_assoc($result1)) { if ($row['id']==$row1['d_id']) { echo '<li><a class="geta" href="listhou2.php?userid='.$row1['id'].'">'.$row1['d_name'].'</a></li>'; } } mysqli_free_result($result1); echo '</ul>'; echo '</li>'; } mysqli_free_result($result);?>
listhou2.php代码
<?php header("Content-Type:text/html;Charset=utf-8");$host='localhost';$user='root';$password='63698000xw';$database='test';$conn=mysqli_connect($host, $user, $password, $database) or die('DataBase connected error<br/>'.mysqli_connect_error($conn)); if (isset($_GET['userid'])) { $row1id=$_GET['userid']; // echo "$row1id"; $query2="SELECT * FROM user WHERE u_id=$row1id"; $result2=mysqli_query($conn,$query2); // echo "我取得ID了"; if (mysqli_num_rows($result2)) { echo '<table border="1">'; echo "<tr><th>姓名</th><th>电话</th></tr>"; while ($row2=mysqli_fetch_assoc($result2)) { echo "<tr><td>{$row2['username']}</td><td>{$row2['tphone']}</td></tr>"; } }else{ echo "<tr><td>没有查到相关记录</td></tr>"; } $num=mysqli_num_rows($result2); echo '<tr><th colspan="2">共取出'.$num.'条数据</th></tr>'; echo '</table>'; mysqli_free_result($result2); }else{ echo "没有获取ID"; } mysqli_close($conn); ?>
回复讨论(解决方案)
是放到
$($(".listshow a").click(function() { $("list2").load($(this).attr("href")); return false;});
1、加上 点击事件 $($(".listshow a").click(function() {
2、在1的处理方法里用ajax调用
3、在listhou2.php页面里接收值,并处理,(可以返回值,也可以没有)
例 :
$.ajax({ type:'get', async : false, url: "{:U('paygoodsAjax')}", data:"ord_id="+ord_id, success: function(msg){ flag= true; //alert(msg) if(msg==1004){ flag= false; $("#jian").html("商品已下架!") $("#jian").css("display",'block'); function times(){ $("#jian").css("display",'none'); clearInterval(timer); } timer=setInterval(times,2500); } else if(msg==1003){ flag= false; $("#jian").html("商品库存不够了") $("#jian").css("display",'block'); function times(){ $("#jian").css("display",'none'); clearInterval(timer); } timer=setInterval(times,2500); }else if(msg==1002){ flag= false; $("#jian").html("订单存在过期促销商品!") $("#jian").css("display",'block'); function times(){ $("#jian").css("display",'none'); clearInterval(timer); } timer=setInterval(times,3000); } //location.reload(); } }); return flag;
后端处理
//ajax 商品支付前判断 public function paygoodsAjax(){ $ord_id=I("get.ord_id"); //echo $ord_id; $ordershopids=M("ordershop")->where("order_id = '$ord_id'")->getFields("shop_id"); //var_dump($ordershopids); //判断商品是否下架或者库存为0 if($ordershopids){ foreach($ordershopids as $id){ $goodsinfo=M("goodsshop")->where("id = '$id'")->find(); //var_dump(M("goodsshop")->getLastSql()); if($goodsinfo){ if($goodsinfo['is_up']!=1){ echo "1004";exit(); } if($goodsinfo['stock']<=0){ echo "1003";exit(); } }else{ echo "1004";exit(); } } }
是放到
$($(".listshow a").click(function() { $("list2").load($(this).attr("href")); return false;});
把这个代码加到页面后直接就一片空白了啊,是这样加吗?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>测试</title><link rel="stylesheet" type="text/css" href="style/index.css" /><script type="text/javascript" src="js/jquery-1.11.3.min.js"></script><script type="text/javascript"> $(function(){ $(".listshow ul").load("listhou.php");});</script></head><body><div class="listshow" ><ul></ul></div><div class="list2"> </div></body></html>
$(".listshow a").click(function() {
$("list2").load($(this).attr("href"));
return false;
});
列表在 listhou.php 产生,所以要写在
$(".listshow ul").load("listhou.php"); 的回调里
即
$(".listshow ul").load("listhou.php", {}, function() {
$(".listshow a").click(function() {
$("list2").load($(this).attr("href"));
return false;
});
});
$(".listshow a").click(function() {
$("list2").load($(this).attr("href"));
return false;
});
列表在 listhou.php 产生,所以要写在
$(".listshow ul").load("listhou.php"); 的回调里
即
$(".listshow ul").load("listhou.php", {}, function() {
$(".listshow a").click(function() {
$("list2").load($(this).attr("href"));
return false;
});
});
麻烦您再给看看还是不传参数
'.$row1['d_name'].'
又要是这样的超链
userid 传不过去?
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="jquery.min.js"></script> <script> $(function(){ $('.listshow').load('list.php', function(){ $('.listshow a').on('click', function(){ $('.list2').load($(this).attr('href')); return false; }); }); }) </script></head><body><div class="listshow"></div><div class="list2"></div></body></html>
<?php// list.phpecho '<a href="2.php?param=1">测个试</a>';
<?php// 2.phpprint_r($_REQUEST);
楼主的代码的list2前少了一个点,估计加载不出来
OK 谢谢各位 ,解决了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
