【问题标题】:Passing onclick Jquery variable issue in php在php中传递onclick Jquery变量问题
【发布时间】:2018-09-28 12:03:55
【问题描述】:

我正在开发 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;

    }
?>

【问题讨论】:

  • $id = $_POST['id']; 在你的 php 中
  • 另外请查看prepared statements 以确保您的查询安全。

标签: php jquery html mysql


【解决方案1】:

使用此代码来获得 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>

【讨论】:

  • 嗨,Shubham Azad,在 php 文件中没有回显,先生。
  • 什么意思,回声不来了?
  • 你得到的是空响应吗?
  • 不,先生,我什么都没得到。
  • 是的先生它正在重新加载先生
【解决方案2】:

您应该$_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();

【讨论】:

  • 嗨 mbharanidharan88,,, 这不是正确的 html 表单,如果 html 表单发布方法意味着可以工作。
  • @rajkumar,你是什么意思?你似乎很困惑。使用$.ajax 并设置type:POST,您已经将值发布到PHP 页面,您只能使用$_POST$_GET(如果它属于type:GET )将其读回
  • 你应该检查this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
相关资源
最近更新 更多