简体   繁体   English

在带有 Ajax 的 jQuery 按钮中添加对 PHP 的调用

[英]Add a call to PHP in a jQuery button with Ajax

I tried to modify the behaviour of the Save button in my DokuWiki ( see my post on their forum without answer yet ).我试图在我的 DokuWiki 中修改Save 按钮的行为( 请参阅我在他们论坛上的帖子,但尚无答案)。

I want to call a specific file.php when the user hits the Save Button , ie every time there is an edit in the wiki.我想在用户点击Save Button时调用一个特定的file.php ,即每次在 wiki 中进行编辑时。 The PHP file recalculates all the connections in the Wiki and creates a new network diagram of the links in-between pages using Rscript. PHP 文件重新计算 Wiki 中的所有连接,并使用 Rscript 创建页面之间链接的新网络图。

I think the best way to do that is to use Ajax when the button is clicked ( see this answer about how to do that).我认为最好的方法是在单击按钮时使用Ajax (有关如何执行此操作,请参阅此答案)。 This is the original jQuery code of the button in DokuWiki:这是 DokuWiki 中按钮的原始 jQuery 代码

// reset change memory var on submit
jQuery('#edbtn__save').on('click',
    function() {
        window.onbeforeunload = '';
        textChanged = false;
    }
);

I would like to add something like:我想添加如下内容:

jQuery('#edbtn__save').on('click',
    function() {
        window.onbeforeunload = '';
        textChanged = false;
        $.ajax({
          url: "./data/forms/file.php",
          }).done(function( msg ) {
            alert( "Data Saved: " + msg );
          });
        }
);

But this is not working yet (I tried different URLs too, but it is not clear what to use as I run DokuWiki on my localhost for the moment).但这还行不通(我也尝试了不同的 URL,但目前尚不清楚在我的本地主机上运行 DokuWiki 时该使用什么)。 I never studied jQuery nor Ajax, so this is new to me.我从未研究过 jQuery 或 Ajax,所以这对我来说是新的。 I don't really understand how it works, but it might just need a simple fix.我真的不明白它是如何工作的,但它可能只需要一个简单的修复。

This is what the file.php looks like:这是 file.php 的样子:

<?php
include("./data/forms/myFunctions.php");
matrixer();
 ?>

PS: the documentation I found about the function on() and the DokuWiki source code . PS:我找到的关于 function on()DokuWiki 源代码的文档。

I found a very useful link this morning.今天早上我发现了一个非常有用的链接 from this I was able to work it out.由此我能够解决它。

So the proper code is:所以正确的代码是:

jQuery('#edbtn__save').on('click',
    function() {
        window.onbeforeunload = '';
        textChanged = false;
        jQuery.ajax({
          type: 'POST',
          url: '/~User/dokuwiki/data/forms/file.php',
          success: function(data) {
              alert(data);
              $("p").text(data);
            }
        });
    }

It works !有用 ! Make sure the file is accessible via browser to avoid Error 403 (Forbidden)确保文件可通过浏览器访问以避免Error 403 (Forbidden)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM