Heim > Backend-Entwicklung > PHP-Tutorial > 关于PHP用jQuery的ajax传参问题,请各位帮帮忙

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

WBOY
Freigeben: 2016-06-20 12:43:20
Original
1268 Leute haben es durchsucht

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

目前树结果已经用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>
Nach dem Login kopieren

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>'.$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);?>
Nach dem Login kopieren


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);    ?>
Nach dem Login kopieren


回复讨论(解决方案)

是放到

里吗?

$($(".listshow a").click(function() {  $("list2").load($(this).attr("href"));  return false;});
Nach dem Login kopieren
Nach dem Login kopieren


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;  
Nach dem Login kopieren


后端处理

//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();                   }               }           }  
Nach dem Login kopieren

是放到

里吗?

$($(".listshow a").click(function() {  $("list2").load($(this).attr("href"));  return false;});
Nach dem Login kopieren
Nach dem Login kopieren


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

$(".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>
Nach dem Login kopieren


<?php// list.phpecho '<a href="2.php?param=1">测个试</a>';
Nach dem Login kopieren


<?php// 2.phpprint_r($_REQUEST);
Nach dem Login kopieren



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

OK 谢谢各位 ,解决了

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage