donglu7286 2015-04-30 08:32
浏览 45
已采纳

Wordpress Ajax表单有两个提交按钮

I am working in wordpress and I have one form and two submit buttons. I am using ajax and it is wordpress.

When first submit button is pressed I want statement 1 to be echoed and when submit button number 2 is pressed I want state 2 be echoed. I have followed the code tutorials but when I press submit from the control returns empty or no result and in inspect there is no error in the browser. Below is my code.

HTML Form

<form id="voteform" action="" method="post">
<input  name='vote' value='two' src='http://localhost:8080/test/wp-content/uploads/2015/04/up.jpg' type='submit' type='image'>
<input  name='vote' value='one' src='http://localhost:8080/test/wp-content/uploads/2015/04/up.jpg' type='submit' type='image'>
</form>

I am not copying the enqueue code but just the actual php function that executes

function articlevote ()
{

  if ($_POST['vote'] == 'one') {
    echo json_encode("1 vote button is pressed");
    die();
  }
  else if ($_POST['vote'] == 'two') {
    echo json_encode("2 vote button is pressed");
    die();  
  }
}

Ajax and jquery

    jQuery(function ($) {
    $(document).on("click","input[name=vote]", function() {
    var _data= $('#voteform').serialize() + '&vote=' + $(this).val();
    $.ajax({
      type: 'POST',
      url: yes.ajaxurl,
      data:_data,
      success: function(html){
         $("#myresult").html(res);
      }
    });
    return false;
  });
});

Again kindly note that I am using wordpress so kindly guide me in this thanks

  • 写回答

2条回答 默认 最新

  • duandingyou3331 2015-04-30 09:02
    关注

    This should get you what you need:

    Html:

    <form id="voteform" action="" method="post">
        <input type="text" name="field1" value="field one value"/>
        <input type="text" name="field2" value="field two value"/>
        <input  class='vote' value='two' src='http://localhost:8080/test/wp-content/uploads/2015/04/up.jpg' type='submit' type='image'/>
        <input  class='vote' value='one' src='http://localhost:8080/test/wp-content/uploads/2015/04/up.jpg' type='submit' type='image'/>
    </form>
    

    jQuery

     jQuery(function($) {
         var yes = {ajaxurl:"mypage.php"}; // created for demo
    
          $('.vote').click(function(e) {
              e.preventDefault(); // stop the normal submit
              var _data = $('#voteform').serialize()+'&vote=' + $(this).val();
              //alert(_data)
              $.ajax({
                  type: 'POST',
                  url: yes.ajaxurl,
                  data: _data,
                  success: function(html) {
                      $("#myresult").html(html); // you had `res` here
                  }
              });
          });
      });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 envi深度学习模块错误原因
  • ¥15 孟德尔随机化混杂因素
  • ¥15 关于react-hook组件用函数控制是否渲染的及时性问题。
  • ¥50 Linux下的软件,要做模块化拆分。进程间通信是否有开源框架可以借用?
  • ¥100 修改原有的MYSQL存储代码,在最右边添加多列数据
  • ¥20 Open Interpreter 使用时报错: still has pending operation at deallocation, the process may crash
  • ¥15 qt中链接动态链接库,调用其中的函数,该函数的参数需要传入回调函数,自己创建的回调函数无法作为参数传递进去
  • ¥15 matlab svm二分类代码问题
  • ¥40 求一款能支持ios15以上的屏蔽越狱插件。比较好用的
  • ¥15 C++ QT对比内存字符(形式不定)