Table of Contents
'.$row['c_name'].'
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于PHP用jQuery的ajax传参问题,请各位帮帮忙

关于PHP用jQuery的ajax传参问题,请各位帮帮忙

Jun 20, 2016 pm 12:43 PM

我想实现的是下图这个功能

目前树结果已经用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>
Copy after login

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);?>
Copy after login


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);    ?>
Copy after login


回复讨论(解决方案)

是放到

里吗?

$($(".listshow a").click(function() {  $("list2").load($(this).attr("href"));  return false;});
Copy after login
Copy after login


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;  
Copy after login


后端处理

//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();                   }               }           }  
Copy after login

是放到

里吗?

$($(".listshow a").click(function() {  $("list2").load($(this).attr("href"));  return false;});
Copy after login
Copy after login


把这个代码加到页面后直接就一片空白了啊,是这样加吗?
<!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>
Copy after login

$(".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>
Copy after login


<?php// list.phpecho '<a href="2.php?param=1">测个试</a>';
Copy after login


<?php// 2.phpprint_r($_REQUEST);
Copy after login



楼主的代码的list2前少了一个点,估计加载不出来

OK 谢谢各位 ,解决了

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

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

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

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

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

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�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

See all articles