douzhuo6270 2014-12-10 17:46
浏览 117

如何将值从jQuery-AJAX函数发送到PHP文件并在PHP文件中访问这些值?

I've written a jQuery-AJAX function as follows :

$('#request_form').submit(function(e) {
  var form = $(this);
  var stud_id = $('#stud_id').val();
  var reg_date = $('#reg_date').val();
  var formdata = false;


  var fileInput = $("#receipt_image")[0];  

  /*I want to pass values of below variables to the PHP file.*/
  var ImgSizeInBytes = fileInput.files[0].size;
  var filename = $('input[type=file]').val().split('\\').pop();
  var customer_id = $('#customer_id').val();
 /*These values need to send to PHP file and access there */

  if(window.FormData) {
    formdata = new FormData(form[0]);
  }

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

  $.ajax({
    url         : 'student_request.php',
    type        : 'POST',    
    cache       : false,
    data        : formdata ? formdata : form.serialize(),
    contentType : false,
    processData : false,
    success: function(response) {
      var responseObject = $.parseJSON(response);    
      if(responseObject.error_message) { 
        if ($(".alert-dismissible")[0]) {
          $('.alert-dismissible').remove();   
        }  
        var htmlString = "<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>"+responseObject.error_message+"</div>";    
        $(htmlString).insertBefore('div.modal-body #request_form');        
      } else { 
        alert("Student successfully registered...!!!");       
      }
    }
  });
  e.preventDefault();
});

Now I'm able to access the values filled in by user on a form by means of $_POST array in PHP file. But I also want to pass the values I put in comment in my code above to the PHP file.

The values/parameters which I want to send are not part of a form fields. I've manipulated the values of these variables. So they can't come in $_POST array.

My issue is how should I send these values to PHP file and how should I access these values in PHP file?

  • 写回答

2条回答 默认 最新

  • dqst96444 2014-12-10 17:54
    关注

    You should change this: formdata ? formdata : form.serialize()

    Store this in a variable and concatenate the values you want to send.

    For Example:

    var pars = formdata ? formdata : form.serialize();
    pars += "&myField1=myValue1&myField2=myValue2"
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题