Rahgav Rahgav - 2 months ago 9
jQuery Question

I want to fetch values from database on button click on same page in PHP

I have some button and I want to fetch values from the database in PHP using that button when user clicks on that button.

<a href="id=<?php echo $query2['email']; ?>"><button class="btn" id="myBtn" style="background-color:#FF3366;color:#CCCCCC;">View More Details</button></a>


This is my PHP code:-

<?php
$query4=mysql_query("SELECT * FROM doctorregister WHERE email='".$_GET['id2']."'");
$query6=mysql_fetch_array($query4);
?>


I have used ajax also.

<button class="btn" id="myBtn" onclick="search_value2()" value="<?php echo $query2['email']; ?>" style="background-color:#FF3366;color:#CCCCCC;">View More Details</button>

<script>
function search_value2(){
var name=$("#myBtn").val();
alert(name);

$.ajax({
type:"GET",
data:"id2="+name,
success:function(data){
}
});
}

</script>


I want to fetch values on the same page.Please Help.

Answer

Use some jquery here.

<button class="btn" id="myBtn" email="<?= $query2['email'] ?>">View More Details</button>

  <script>
  $(document).ready(function(){
    $("#myBtn").click(function(){
        var email = $(this).attr('email');
          $.ajax({
          type: 'POST',
          data:{email : email},
          success: function(data) {
              console.log(data)
          }
        });
    });
  });
  </script> 

On top of the page, you can check for

<?php
  if (isset($_POST['email'])) {
      echo "Email : " . $_POST['email'];
      //Or
     $email = $_POST['email'];
     //Do stuff with this variable in php..
  }
  ?>
Comments