Javascript 通过ajax提交表单数据不起作用

Javascript 通过ajax提交表单数据不起作用,javascript,php,jquery,ajax,forms,Javascript,Php,Jquery,Ajax,Forms,很抱歉这个愚蠢的问题,但我似乎无法让它进行,我想我最好给你更多的信息比不够- 我有一个在php循环中运行的表单,如下所示: <form name="primaryTagForm'.$post->ID.'" id="primaryTagForm'.$post->ID.'" method="POST" enctype="multipart/form-data" > <fieldset class="tags"> <label for="pos

很抱歉这个愚蠢的问题,但我似乎无法让它进行,我想我最好给你更多的信息比不够-

我有一个在php循环中运行的表单,如下所示:

<form name="primaryTagForm'.$post->ID.'" id="primaryTagForm'.$post->ID.'" method="POST" enctype="multipart/form-data" >
  <fieldset class="tags">
      <label for="post_tags'.$post->ID.'">Tags:</label>
      <input type="text" value="" tabindex="35" name="postTags'.$post->ID.'" id="postTags'.$post->ID.'" />
  </fieldset>

  <fieldset>
      <input type="hidden" name="submitted" id="submitted" value="true" />
      '.wp_nonce_field( 'post_nonce', 'post_nonce_field' ).'
      <button class="button" type="submit">Tag</button>
  </fieldset>
</form>
jQuery(document).ready(function($) {
    $(".button").click(function(e) {
        e.preventDefault();
        var $target = $(e.target),
            $form = $target.closest('form');
    $.ajax({
        type: "POST",
        url: $form.prop('action'),
        data: $form.serialize(),
        success:  function(data){
            //alert("---"+data);
            alert("Tags have been updated successfully.");
        }
    });
    });
});
因此,当我尝试运行这个程序时,通过查看控制台会发生一些事情,如果我在其中一个表单上点击submit,那么页面上的所有表单都会发送到tags-ajax.php(我确信这是因为我在循环中执行此操作,但不确定如何执行,并在tags-ajax.php上引入post->ID)

第二,也是最重要的一点是,实际上没有任何东西可以保存,我单击“标记”但是(提交),我会收到那些成功警报(不幸的是,每次发布都会收到),但是当我单击这些标记时,这些标记实际上并没有保存

我的问题:如何使用ajax/php实际保存数据,如何在不重新加载页面的情况下进行后期刷新,以便用户看到这些数据实际上已添加

最新更新:在进行了下面提到的序列化编辑之后,我提交了我的表单并检查了控制台,发现post方法出现了500个内部服务器错误。。我在想,我的问题是否是因为我有一个表单和一个内联脚本,其中ajax在一个循环中运行?因此,从技术上讲,一个页面上有20个帖子/表单/内联脚本,当您提交其中一个时,它们都提交了,这可能会导致500个内部错误?

使用

你必须改变

data: "primaryTagForm'.$post->ID.'",


ajax中的data:选项应该是

data: $("#primaryTagForm'.$post->ID.'").serialize(),

简化标记。您不必到处使用
id
属性。只需在表单中包含一个hidenn标记,其值为
$post->id
。还要在表单的acton属性处回显ajax url

因此,html应该与此类似:

<form method="POST" action="' . get_stylesheet_directory_uri() .'/tags-ajax.php" >
  <input type='hidden" name="id" value="'.$post->ID.'">
  <fieldset class="tags">
      <label>Tags:</label>
      <input type="text" value="" tabindex="35" name="tags" />
  </fieldset>

  <fieldset>
      <input type="hidden" name="submitted" id="submitted" value="true" />
      '.wp_nonce_field( 'post_nonce', 'post_nonce_field' ).'
      <button class="button" type="submit">Tag</button>
  </fieldset>
</form>

别忘了使用“#”啊,谢谢,我试过了,并且在我的控制台“my domain/tags-ajax.php 500 Internal Server Error”中得到了这个消息,不知道为什么它不起作用?可能是tags-ajax.php文件中的语法错误检查一下。检查控制台中的错误行。错误:函数(req,status,err){console.log(req.responseText);}成功后添加此项:在ajax中,然后在控制台中,您将得到错误,请在控制台中提供错误详细信息。我希望我能帮助你。感谢您的关注,我想知道我是否可以绕过tags-ajax.php文件,将wp_set_object_terms函数作为my.ajax数据传递进来:……或者我应该使用$来代替.ajax数据中当前的内容(这是)
data: $("#primaryTagForm'.$post->ID.'").serialize(),
<form method="POST" action="' . get_stylesheet_directory_uri() .'/tags-ajax.php" >
  <input type='hidden" name="id" value="'.$post->ID.'">
  <fieldset class="tags">
      <label>Tags:</label>
      <input type="text" value="" tabindex="35" name="tags" />
  </fieldset>

  <fieldset>
      <input type="hidden" name="submitted" id="submitted" value="true" />
      '.wp_nonce_field( 'post_nonce', 'post_nonce_field' ).'
      <button class="button" type="submit">Tag</button>
  </fieldset>
</form>
jQuery(document).ready(function($) {
    $(".button").click(function(e) {
        e.preventDefault();
        var $target = $(e.target),
            $form = $target.closest('form');
    $.ajax({
        type: "POST",
        url: $form.prop('action'),
        data: $form.serialize(),
        success:  function(data){
            //alert("---"+data);
            alert("Tags have been updated successfully.");
        }
    });
    });
});