0

I'm working on Core php, i'm on click of div data is coming and i store a variable and i want pass check with database query it it is matching will come all the records how to do this please help any one.

Html code:

<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 code:

<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 code:

<?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;

    }
?>
rajkumar
  • 19
  • 5

2 Answers2

0

Use this this code to get result in your ajax success.

<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
  • 677
  • 8
  • 22
-1

You should $_POST to get the posted parameters in PHP

use

$id =  $_POST["id"] 

Also, use PreparedStatement to avoid 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();
Hary
  • 5,208
  • 6
  • 31
  • 67
  • Hi mbharanidharan88,,, This is not html form correct, if html form post method means will work your. – rajkumar Sep 28 '18 at 12:17
  • @rajkumar, What you meant? You're confused it seems. With `$.ajax` and setting `type:POST` you're already posting values to PHP page and you can it read it back only with `$_POST` or `$_GET` if it is of `type:GET ` – Hary Sep 28 '18 at 12:20
  • You should check [this](https://stackoverflow.com/questions/6009206/what-is-ajax-and-how-does-it-work) – Hary Sep 28 '18 at 12:22