PHP / HTML,AJAX未调用/执行PHP文件

问题描述 投票:0回答:1

在我提出这个问题之前,我在一个解决方案中搜索了整个互联网。也许是我还是我的解决方案。抱歉。

情况是,我有一个Web应用程序,该应用程序是用PHP构建的,并通过函数与数据库进行交互。每个页面都是在其中执行html代码的php。

问题是,当我要实现Ajax以更新数据而不刷新页面时,单击该按钮时未执行PHP文件。

javascript具有两个功能:

  1. 警告按钮的序列化数据(有效)
  2. 使用Ajax运行php脚本(无效)

HTML标头

<!DOCTYPE html>
        <html>
            <head>
                <meta charset="utf-8" />
                <meta http-equiv="X-UA-Compatible" content="IE=edge">
                <meta name="viewport" content="width=device-width, initial-scale=1">

                <!-- JQuery -->
                    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
                    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous">

我正在使用引导表,该列之一是具有以下内容的按钮:

<form id="form" class="favourite-form" method="post"><input style="display:none" type="hidden" id=5 name="subject_id" value=5 ><input type="submit" name="submit" value="Button"></form><div id="response"></div>

单击按钮时,将弹出以下警报:enter image description here

脚本文件包含在php文件中:

include("favorite_ajax.html");

脚本文件具有以下内容:

$(document).on('submit', '.favourite-form', function(e) { 
        e.preventDefault();             //prevent a normal postback and allow ajax to run instead
        var data = $(this).serialize(); //"this" represents the form element in this context
        alert(data);   // this alert works

    $.ajax({       
        method:   "POST", 
        url:      "process.php",  // calling this php file doens't work
        data:     data,
        success: function(data) { 
          alert("Data Save: " + data); 
        },
        error: function(jqXHR, textStatus, errorThrown) //gracefully handle any errors in the UI
        {
          alert("An ajax error occurred: " + textStatus + " : " + errorThrown);
        }
      }); 
}); 

最后,php文件具有一个简单的函数,可以插入(虚拟)数据。 php文件已经过测试,不包含任何错误。

<?php
        insertDummyData(1, 123); 
?>

在另一个主题中,我还阅读了检查日志。当我按下按钮时,日志中也没有添加任何记录。

我很着急,我做错了什么?花几天的时间来解决这个问题,直到现在没有结果:(

javascript php jquery html ajax
1个回答
1
投票
  1. 将错误日志放入“ process.php”中,以确保它实际上已被调用,如果未如其他人所建议的那样被调用,则可能存在路径,权限或脚本错误。
  2. insertDummyData / process.php实际做什么?它必须回显某种东西,就像它正在写页面以进行AJAX调用一样,以获得非空结果。例如,在process.php

    <?php 
    $new_data = my_function_which_does_stuff_to_posted_data($_POST['data']);
    echo json_encode($new_data); 
    exit(); 
    
© www.soinside.com 2019 - 2024. All rights reserved.