代码之家  ›  专栏  ›  技术社区  ›  I Am Stack

ajax使用函数或数组发布不同的数据

  •  -1
  • I Am Stack  · 技术社区  · 5 年前

    实际上,我正在寻找一种更好的方法来为多个用户使用ajax向php提交数据。请看我的密码。

    $(document).ready(function(){
      $("#all").click(function(){
        document.getElementById('babon').click();
        document.getElementById('af').click();
        document.getElementById('anik').click();
        document.getElementById('abir').click();
        document.getElementById('aurko').click();
      });
    
      $("#babon").click(function(){
        var code = $('#rcode').val();
        $('span.babon').text("Sending..");
        $.post("redeem.php", //Required URL of the page on server
               {"redeemcode":code,"gameid":"4548211","vcode":"87856"},
               function(response,status){ // Required Callback Function
          var result = jQuery.parseJSON( response );
          $('span.babon').text(result.message);
        });
      });
    
      $("#anik").click(function(){
        var code = $('#rcode').val();
        $('span.anik').text("Sending..");
        $.post("redeem.php", //Required URL of the page on server
               {"redeemcode":code,"gameid":"54489231","vcode":"56465"},
               function(response,status){ // Required Callback Function
          var result = jQuery.parseJSON( response );
          $('span.anik').text(result.message);
        });
      });
    
      $("#abir").click(function(){
        var code = $('#rcode').val();
        $('span.abir').text("Sending..");
        $.post("redeem.php", //Required URL of the page on server
               {"redeemcode":code,"gameid":"8798456","vcode":"78542"},
               function(response,status){ // Required Callback Function
          var result = jQuery.parseJSON( response );
          $('span.abir').text(result.message);
        });
      });
    
      $("#aurko").click(function(){
        var code = $('#rcode').val();
        $('span.aurko').text("Sending..");
        $.post("redeem.php", //Required URL of the page on server
               {"redeemcode":code,"gameid":"548745612","vcode":"24875"},
               function(response,status){ // Required Callback Function
          var result = jQuery.parseJSON( response );
          $('span.aurko').text(result.message);
        });
      });
    
      $("#af").click(function(){
        var code = $('#rcode').val();
        $('span.af').text("Sending..");
        $.post("redeem.php", //Required URL of the page on server
               {"redeemcode":code,"gameid":"9887254","vcode":"82499"},
               function(response,status){ // Required Callback Function
          var result = jQuery.parseJSON( response );
          $('span.af').text(result.message);
        });
      });
    });
    

    https://codepen.io/anon/pen/wRNLQO

    请参阅屏幕截图中的输出。 enter image description here

    我使用不同的ajax函数向php提交数据。有没有办法从数组中提交数据?所以代码看起来会更短更漂亮?

    3 回复  |  直到 5 年前
        1
  •  2
  •   Liam    5 年前

    只需编写一个可重用函数:

    var sendingfunction = function(selector, gameid, vcode){
        var code = $('#rcode').val();
        $('span.'+selector).text("Sending..");
        $.post("redeem.php", //Required URL of the page on server
               {"redeemcode":code,"gameid":gameid,"vcode":vcode},
               function(response,status){ // Required Callback Function
                   var result = jQuery.parseJSON( response );
                   $('span.'+selector).text(result.message);
        });
    }
    
    $("#anik").click(function(){
        sendingfunction('anik', "54489231", "56465");
      });
    
    $("#abir").click(function(){
        sendingfunction('abir', "54489231", "56465");
      });
    ...etc.
    
        2
  •  1
  •   hamed hossani    5 年前
    function callPostAjax(element, code, gameId, vCode) {
        $(element).text("Sending..");
        $.post("redeem.php", //Required URL of the page on server
             { "redeemcode": code, "gameid": gameId, "vcode": vCode },
             function (response, status) { // Required Callback Function
                 var result = jQuery.parseJSON(response);
                 $(element).text(result.message)
             }).fail(function (xhr, status, error) {
                 $(element).text('error');
                 console.error(error);
             });
    }
    $("#babon").click(function () {
        var code = $('#rcode').val();
        callPostAjax('span.babon', code, "4548211", "87856");
    });
    
    $("#anik").click(function () {
        var code = $('#rcode').val();
        callPostAjax('span.anik', code, "54489231", "56465");
    });
    
    $("#abir").click(function () {
        var code = $('#rcode').val();
        callPostAjax('span.abir', code, "8798456", "78542");
    });
    
    $("#aurko").click(function () {
        var code = $('#rcode').val();
        callPostAjax('span.aurko', code, "548745612", "24875");
    });
    
    $("#af").click(function () {
        var code = $('#rcode').val();
        callPostAjax('span.af', code, "9887254", "82499");
    });
    
        3
  •  0
  •   Kehinde Ladipo    5 年前

    你需要首先考虑 redeem.php 支持多条记录。如果是,则给出所有相同的“类”记录,并使用 .each() 关键字并在提交之前填充请求数据。