如何通过AJAX调用PHP脚本?

时间:2019-11-02 11:49:17

标签: php

我正在尝试通过AJAX调用PHP脚本,但似乎有一个问题,我不知道它是什么!我尝试使用其他解决方案,但我不太了解它们。 我真正想做的是通过AJAX代码调用PHP脚本,然后使用返回的数据更新标头中的回显。 到目前为止,这是我所做的:

Header.php

<?php session_start(); ?>
<script src="https://code.jquery.com/jquery-3.4.1.slim.js" integrity="sha256-BTlTdQO9/fascB1drekrDVkaKd9PkwBymMlHOiG+qLI=" crossorigin="anonymous"></script>
<?php require_once('db/dbconnection.php'); ?>
<?php
    $result = $mysqli->query("SELECT * FROM task
    INNER JOIN user ON task.user_id = user.id
    WHERE username='$username' AND status = 'achieved'") or die($mysqli->error);

    $count = mysqli_num_rows($result);
?>
    <div class="done" data-tooltip="Achieved">
        <span><?php echo $count; ?></span>
    </div>

Dashboard.php

<link rel="stylesheet" href="css/dashboard.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.js" integrity="sha256-BTlTdQO9/fascB1drekrDVkaKd9PkwBymMlHOiG+qLI=" crossorigin="anonymous"></script>

<?php require_once('db/dbconnection.php'); ?>
<?php require_once("header.php"); ?>
<?php $result = $mysqli->query("SELECT * FROM `task`") or die($mysqli->error); ?>
<?php 
    while ($row = mysqli_fetch_array($result)):
?>
    <input type="checkbox" name="checkbox[]" value="<?php echo utf8_encode($row['id']); ?>">
    <p id="task"><?php echo utf8_encode($row['task']); ?></p>
<?php endwhile; ?>

<!-- js files -->
<script src="js/main.js"></script>

dashboard.css

input[type=checkbox]:checked~p {
text-decoration: line-through;
}

main.js

$(document).ready(function() {
   $("input[type='checkbox']").on('change', function() {
   var id = $(this).val();
   if (id) {
     $.ajax({
       type:'POST',
       url:'db/ajaxdata.php',
       data:'id='+id,
       success:function(html){
         $(".done").html(html);
       }
     });
   } else {
     //Error msg
   }
 });
});

ajaxdata.php

session_start();

require_once('dbconnection.php');

if ((isset($_POST['id'])) && (!empty($_POST['id']))){

    //Example
    $count = 2;
    echo "$count";

} else {
    header("location: /error.php");
}

对不起,如果我没有正确解释问题!

4 个答案:

答案 0 :(得分:1)

您解释得很好:-)

  1. 在ajax(main.js)中使用 error 回调函数,因此您可以看到错误即将来临
  $.ajax({
       type:'POST',
       url:'db/ajaxdata.php',
       data:'id='+id,
       success:function(html){
         $(".done").html(html);
       },
       error:function(html){
         $(".error").html(html);
         console.log(html);
       }
});
  1. 确保将ajax请求重定向到绝对路径ex。 http://localhost/ajaxdata.php

  2. 在浏览器中打开远程URL(http://localhost/ajaxdata.php)并查看得到的结果,可能是数据库连接文件中有错误。

  3. 使用exit();在ajax中检查

if ((isset($_POST['id'])) && (!empty($_POST['id']))){

    //Example
    $count = 2;
    echo $count;
    exit();

} else {
    header("location: error.php");
}

答案 1 :(得分:1)

将main.js编辑为this.i已更改数据和成功

中的代码
$(document).ready(function() {
   $("input[type='checkbox']").on('change', function() {
   var id = $(this).val();
   if (id) {
     $.ajax({
       type:'POST',
       url:'db/ajaxdata.php',
       data:{
          id: id
       },
       success:function(data){
         $(".done").html(data);
       }
     });
   } else {
     //Error msg
   }
 });
});

答案 2 :(得分:1)

尝试一下:

$(document).ready(function() {
       $("input[type='checkbox']").on('change', function() {
       var id = $(this).val();
       if (id) {
         $.ajax({
           type:'POST',
           url:'db/ajaxdata.php',
           data:{id:id},
           success:function(response){
             $(".done").html(response);
           }
         });
       } else {
         //Error msg
       }
     });
    });

答案 3 :(得分:0)

我已经解决了问题。这是jQuery的苗条版本中的一个问题! 不包括ajax 。只需将其更改为未压缩版本,即可解决问题。