Javascript 如何:PHP Ajax页面更新

Javascript 如何:PHP Ajax页面更新,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我有一个输入字段和两个按钮来添加/删除数据库中的单词。 我正在使用3个php文件。输出html代码的主文件addtag.php可以添加单词,removetag.php可以删除单词 我想在单击加号时调用addtag.php并发送输入字段的内容。单击减号时应调用removetag.php addtag.php和removetag.php应该在后台运行,页面应该只更新 以下元素在同一页面上多次列出。有不同的链接和值,但元素是相同的 <!-- language-all: lang-html --&

我有一个输入字段和两个按钮来添加/删除数据库中的单词。 我正在使用3个php文件。输出html代码的主文件addtag.php可以添加单词,removetag.php可以删除单词

我想在单击加号时调用addtag.php并发送输入字段的内容。单击减号时应调用removetag.php

addtag.php和removetag.php应该在后台运行,页面应该只更新

以下元素在同一页面上多次列出。有不同的链接和值,但元素是相同的

<!-- language-all: lang-html -->
<bdiv>
<div>
<a href="001.mp4"><img src="001.jpg" /></a>
</div>
<tagbox>
<form>
<input type="text" id="tag" name="tag">
<input type="hidden" id="hash" name="hash" value="23547">
<button class="button" type="submit" formaction="addtag.php" method="GET">+</button>
<button class="button" type="submit" formaction="removetag.php">-</button>
</form>
<tagboxtxt>foo bar</tagboxtxt>
</tagbox>
</bdiv>

<bdiv>
<div>
<a href="002.mp4"><img src="002.jpg" /></a>
</div>
<tagbox>
<form>
<input type="text" id="tag" name="tag">
<input type="hidden" id="hash" name="hash" value="67889">
<button class="button" type="submit" formaction="addtag.php" method="GET">+</button>
<button class="button" type="submit" formaction="removetag.php">-</button>
</form>
<tagboxtxt>bla huh</tagboxtxt>
</tagbox>
</bdiv>

您缺少捕获点击“加号”按钮并调用ajax函数的javascript。尝试将此javascriptsnippet粘贴到函数下面:

//Execute on page ready
$(function() {
    $('body').on('click', '.plusSign', function() {
        addTag();
    });
});

我会调用一个js文件,让它引用两个添加和删除php文件——我已经在输出html的主文件中为您模拟了这个文件

<head>
    <script type='text/javascript'></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.0.min.js"></script>
    <script type="text/javascript" src="tag.js"></script>
</head>

<tagbox>
    <input type="text" name="tag">
    <div class="cssCircle plusSign" id="plus">&#43; </div>
    <div class="cssCircle minusSign" id="minus" style="position:absolute; top:20px; left:25px;">&#8211;</div>

</tagbox>

这应该可以满足您的需要

您可以发布您已经尝试过的代码以发送AJAX请求吗?@StuntHacks我已经添加了我使用的代码。我用它做了一些测试,但我想我缺少一些技能。什么不起作用?警报中写了什么?您忘了提到您正在使用jQuery。您已经描述了您希望ti如何工作,但是没有提供任何关于您所实现的代码如何工作的信息。就目前而言,这个问题没有答案。@symcbean我不知道JQuery是否是最好的解决方案。首先,我需要将输入字段的值发送到php文件,该文件将该值转换为变量并在后台运行。谢谢!我已经更新了我的问题。这应该能更好地解释我在找什么。
<head>
    <script type='text/javascript'></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.0.min.js"></script>
    <script type="text/javascript" src="tag.js"></script>
</head>

<tagbox>
    <input type="text" name="tag">
    <div class="cssCircle plusSign" id="plus">&#43; </div>
    <div class="cssCircle minusSign" id="minus" style="position:absolute; top:20px; left:25px;">&#8211;</div>

</tagbox>
$(document).ready(function() {
$("#plus").click(function() {
        $.ajax({
            type: "POST",
            url: "addtag.php",
            dataType: "json",
            data: ({word: $("#tag").val(),}),
            success:function(result){
                alert(result);
                                    }
        });

});

$("#minus").click(function() {
        $.ajax({
            type: "POST",
            url: "removetag.php",
            dataType: "json",
            data: ({word: $("#tag").val(),}),
            success:function(result){
                alert(result);
                                    }
        });

});
});