0

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.

2
  • what error you are getting? can you show you browser network tab screen shot while loding the page Commented Aug 13, 2017 at 13:03
  • issue is here : $d =$_POST['iD']; your request is not post request Commented Aug 13, 2017 at 13:05

1 Answer 1

2

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;

?>
Sign up to request clarification or add additional context in comments.

2 Comments

It still doesn't work. I say error on line 4:test.html:4 Uncaught ReferenceError: $ is not defined
have you include jquery?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.