笑故挽风 2017-06-13 18:32 采纳率: 100%
浏览 17

求助:PHP Ajax页面更新

我有一个输入字段和2个按钮,用于从数据库中添加/删除单词。

我正在使用3个php文件:输出html代码的主文件,可以添加单词的addtag.php和可以移除单词的removetag.php文件。

我想在单击加号时调用addtag.php并发送输入字段的内容,单击减号时应调用removetag.php。addtag.php和removetag.php应该在后台运行,并且页面应该只更新<tagboxtxt>。

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

<!-- 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可以用,但我无法使其正常工作。

我尝试使用下面的函数,但我应该如何在示例中使用它?

function addtag () {
      $.ajax({
        url:"addtag.php",
        type: "POST",
        success:function(result){
         alert(result);
       }
     });
 }
  • 写回答

2条回答 默认 最新

  • ~Onlooker 2017-06-13 19:02
    关注

    You're missing the javascript that captures the click on the 'plus' button, and calls your ajax function. Try pasting this javascriptsnippet below your function:

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

报告相同问题?