简体   繁体   English

带有两个提交按钮的Wordpress Ajax表单

[英]Wordpress Ajax Form with two submit buttons

I am working in wordpress and I have one form and two submit buttons. 我在wordpress中工作,我有一个表单和两个提交按钮。 I am using ajax and it is wordpress. 我正在使用ajax,它是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. 当按下第一个提交按钮时,我希望语句1被回显,当按下提交按钮2时,我希望状态2被回显。 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. 我遵循了代码教程,但是当我从控件中按Submit时,返回空或没有结果,并且在检查中浏览器中没有错误。 Below is my code. 下面是我的代码。

HTML Form HTML表格

<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 我不复制排队代码,而只是复制执行的实际php函数

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 Ajax和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 再次请注意,我正在使用wordpress,因此在此感谢您的指导。

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

This should get you what you need: 这应该为您提供所需的东西:

Html: 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的

 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
              }
          });
      });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM