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

时间:2014-03-20 13:02:53

标签: 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="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>

我已经尝试在该表单下添加我的ajax(仍然在循环中以便我可以获取post_id)并且在我的控制台中我的tag-ajax.php文件发布得很好。以下是我对此的微弱尝试:Save data through ajax jQuery post with form submit和其他类似的问题。

<script>
jQuery(document).ready(function($) {
    $(".button").click(function(e) {
        e.preventDefault();
    $.ajax({
        type: "POST",
        url: "'.get_stylesheet_directory_uri().'/tags-ajax.php",
        data: "primaryTagForm'.$post->ID.'",
        success:  function(data){
            //alert("---"+data);
            alert("Tags have been updated successfully.");
        }
    });
    });
});
</script>

最后这里是我的tags-ajax.php文件中的内容 -

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

        wp_set_object_terms( $post->ID, explode( ',', $_POST['postTags'.$post->ID] ), 'product_tag', true );

        echo'Success!';
}

因此,当我尝试运行时,通过在控制台中查看会发生一些事情,如果我在其中一个表单上点击提交,那么它们在该页面上的所有表单都会发布到标签-ajax.php(我确定这是因为我我是在循环中这样做,但不知道如何做到这一点,并在标签上引入&gt; ID-ajax.php)

第二个,最重要的是没有任何实际存储,我点击&#34;标记&#34;但是(提交)并且我得到了那些成功提醒(不幸的是每个帖子),但是当我点击这些时,标签实际上并没有保存。

我的问题:如何使用ajax / php实际保存数据?如何在不重新加载页面的情况下刷新该帖子,以便用户看到它们实际上已添加?

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

3 个答案:

答案 0 :(得分:1)

ajax中的数据:选项应该是

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

答案 1 :(得分:0)

使用serialize

你必须改变

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

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

答案 2 :(得分:0)

简化您的标记。您不必在任何地方使用id属性。只需在表单中包含值为$post->id的hidenn标记即可。同样以表格的acton属性回显ajax网址。

所以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>

然后你可以使用这样的脚本:

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.");
        }
    });
    });
});
相关问题