简体   繁体   English

在以下情况下如何在$ .ajax()函数的data属性中发送参数?

[英]How to send a parameter in data attribute of $.ajax() function in following scenario?

I've written one AJAX function code as follows : 我编写了一个AJAX功能代码,如下所示:

$('#form').submit(function(e) {
  var form = $(this);
  var formdata = false;
  if(window.FormData) {
    formdata = new FormData(form[0]);
  }

  var formAction = form.attr('action');

  $.ajax({
    type        : 'POST',
    url         : 'manufacturers.php',
    cache       : false,
    data        : formdata ? formdata : form.serialize(),
    contentType : false,
    processData : false,

    success: function(response) {
      if(response != 'error') {
        //$('#messages').addClass('alert alert-success').text(response);
        // OP requested to close the modal
        $('#myModal').modal('hide');
      } else {
        $('#messages').addClass('alert alert-danger').text(response);
      }
    }
  });
  e.preventDefault();
});

Now here in data attribute I want to send some additional parameters with values in data attribute. 现在在这里,我想在data属性中发送一些其他参数,并在data属性中使用值。 How should I send these parameters to PHP file? 我应该如何将这些参数发送到PHP文件?

For clear understanding of my issue refer the following AJAX function code that I've written previously : 为了清楚地了解我的问题,请参考以下我先前编写的AJAX功能代码:

function GetPaymentRequest(status){
     var status = $('#status_filter').val();
     $.ajax({ 
      type: "POST",
      url: "view_payment_request.php",
      data: {'op':'payment_request_by_status','request_status':status},  

      success: function(data) {
      // alert(data);

        }

    });
 }

In above function code you can see that I've passed few parameters with values viz. 在上面的功能代码中,您可以看到我传递了几个带有值viz的参数。 'op':'payment_request_by_status','request_status':status in data attribute. 数据属性中的“ op”:“ payment_request_by_status”,“ request_status”:状态

Exactly same parameters I want to pass in first AJAX function code. 我想在第一个AJAX功能代码中传递完全相同的参数。 The already mentioned parameter "formdata ? formdata : form.serialize()" should also be there. 已经提到的参数“ formdata?formdata:form.serialize()”也应该存在。

How should I do this? 我应该怎么做? Can someone please help me in this regard? 有人可以在这方面帮助我吗?

Thanks in advance. 提前致谢。

Add by using $.param 使用$ .param添加

form.serialize() + '&' + $.param({'op':'payment_request_by_status','request_status':status});

or use serializeArray() and push new items 或使用serializeArray()并推送新项目

var data = form.serializeArray();
data.push({name:'op',value:'payment_request_by_status'}).push({name:'request_status',value:status});

then pass data 然后传递数据

What you can do is, add two hidden fields to your already existing form, name one of them as op and set the value as payment_request_by_status and another one as request_status and the value based on the status. 您可以做的是,将两个hidden字段添加到您已经存在的表单中,将其中一个命名为op ,并将值设置为payment_request_by_status ,将另一个设置为request_status ,然后根据状态设置该值。

When the form is serialized, it will automatically send these values also. 表单被序列化后,它还将自动发送这些值。

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

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