在PHP中传递onclick jQuery变量问题

时间:2018-09-28 12:03:56

标签: php jquery html mysql

我正在使用Core php,我正在单击div数据,并且我存储了一个变量,并且我希望通过数据库查询通过它是否匹配将匹配所有记录如何执行此操作,请帮助一个。

HTML代码:

<div class="col-lg-3 col-md-6  test" data-idtest="Diabetes">
    <a href="" class="box_cat_home">
        <i class="icon-info-4"></i>
        <img src="assets/img/icon_cat_3.svg" width="60" height="60" alt="">
        <h3>Diabetes</h3>
        <ul class="clearfix">
            <li><strong>124</strong>Doctors</li>
            <!-- <li><strong>60</strong>Clinics</li> -->
        </ul>
    </a>
</div> 

jQuery代码:

<script type="text/javascript">
    $(document).ready(function() { 
        $('.test').click(function (){
            var id = $(this).data('idtest') ; 
            //alert(id);
    $.ajax({
         url : "getting.php",
         type: 'POST',
         data    : {id:id },
          }).done(function(response) {
        });
      })
    });
</script>

Php代码:

<?php
echo "ejejejej";
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'mysql';
$dbName = 'fre';

$id =$id;
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

      $men ="select * from tbl_users where doctor_speciality = $id";
      $men_result=$db->query($men);
      $projects=array();
      while($row=mysqli_fetch_assoc($men_result)){ 
      $projects[] = $row;

    }
?>

2 个答案:

答案 0 :(得分:0)

使用此代码获得成功的ajax结果。

<div class="col-lg-3 col-md-6  test" data-idtest="Diabetes">
    <a href="javascript:void(0);" class="box_cat_home">
        <i class="icon-info-4"></i>
        <img src="assets/img/icon_cat_3.svg" width="60" height="60" alt="">
        <h3>Diabetes</h3>
        <ul class="clearfix">
            <li><strong>124</strong>Doctors</li>
            <!-- <li><strong>60</strong>Clinics</li> -->
        </ul>
    </a>
</div> 


<?php

$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'mysql';
$dbName = 'fre';

$id =$_REQUEST['id'];
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

      $men ="select * from tbl_users where doctor_speciality = $id";
      $men_result=$db->query($men);
      $projects=array();
      while($row=mysqli_fetch_assoc($men_result)){ 
      $projects[] = $row;

    }
    if($projects){
       $result=array(
       "responseCode" =>1,
       "message" =>"Data found",
       "data" => $projects
       );
    }else{
       $result=array(
       "responseCode" =>0,
       "message" =>"No data found regarding this id"
       );
    }
echo json_encode($result);die();
?>
<script type="text/javascript">
    $(document).ready(function() { 
        $('.test').click(function (){
            var id = $(this).data('idtest') ; 
            //alert(id);
        $.ajax({
        type: "POST",
         url : "getting.php",
        data    : {id:id },
        cache: false,
        success: function(data){
           console.log(data);
        }
      });
      })
    });
</script>

答案 1 :(得分:-1)

您应该$_POST来获取PHP中发布的参数

使用

$id =  $_POST["id"] 

另外,请使用PreparedStatement来避免SQL Injection

$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
$stmt = $db->prepare("select * from tbl_users where doctor_speciality = ?");
$stmt->bind_param('s', $id));
$stmt->execute();