Raghav Patnecha
Raghav Patnecha

Reputation: 736

Get data from other php file using javascript

I have two files test.html and test.php. What I am trying to do is show data from the PHP file to the HTML using java script. Test.php:

<?php
  $con = mysqli_connect('localhost','root','');
  if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
  }
  mysqli_select_db($con,"autofetch");
  $sql="SELECT * FROM sports ";
  $result = mysqli_query($con,$sql);
  $d =$_POST['iD'];
  if($d==1)
  {
    echo '<div id="container"></div>';
    echo '<div id="blocker"  style="display: none;"></div>';
    $response='';
    while($row=mysqli_fetch_array($result)) {
      $response = $response . " <li class='unread'>" .

        "<h4>". $row["id"] . "</h4>" . 
        "<p>" . $row["sportname"]  . 
        "</li>";
    }
    echo $response;
  }
?>

Test.html:

 <html>
 <head>
 <script>
   $( window ).load(function() {
     var url ='http://localhost/auto/test.php';
     $.get(url,function(data){
     $('#summary').html(data).show(1000);});
   });
</script>
</head>
<body>
  <div  id="summary">
  </div>
</body>
</html>

Also I wanted to load the data on window load. I don't know where I am going wrong. Everything seems fine.

Upvotes: 0

Views: 45

Answers (1)

B. Desai
B. Desai

Reputation: 16436

Remove $d =$_POST['iD']; and condition is not necessory. If you want it. Then you have to use $.post in jquery with passing iD as data

<?php
$con = mysqli_connect('localhost','root','');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"autofetch");
$sql="SELECT * FROM sports ";
$result = mysqli_query($con,$sql);

echo '<div id="container"></div>';
echo '<div id="blocker"  style="display: none;"></div>';
$response='';
  while($row=mysqli_fetch_array($result)) {
$response = $response . " <li class='unread'>" .

"<h4>". $row["id"] . "</h4>" . 
"<p>" . $row["sportname"]  . 
"</li>";
 }
echo $response;

?>

Upvotes: 2

Related Questions