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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵