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

我编写了一个jQuery-AJAX函数,如下所示:</ p>

 <  code> $('#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];

/ 我想传递的值 以下变量到PHP文件。 /
var ImgSizeInBytes = fileInput.files [0] .size;
var filename = $('input [type = file]')。val()。split('\' ).pop();
var customer_id = $('#customer_id')。val();
/ 这些值需要发送到PHP文件并访问那里 /

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

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

$ .ajax({
url:'student_request.php ',
类型:'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 =“&lt; div class ='alert alert-danger alert-dismissible'role ='alert'&gt;&lt; button type ='button'class ='close'data-dismiss ='alert' 咏叹调隐藏= '真' &GT;&安培;倍;&LT; /按钮&gt; “中+ responseObject.error_message +” &LT; / DIV&gt;“中;

$(htmlString).insertBefore('div.modal-body #request_form');

}其他{
alert(“学生成功注册...... !!!”);

}
}
});
e.preventDefault();
});
</ code> </ pre>

现在我可以访问这些值了 用户通过PHP文件中的 $ _ POST </ code>数组填写表单。 但是我也希望将上面代码中的注释值传递给PHP文件。</ p>

我要发送的值/参数不是表单字段的一部分。 我操纵了这些变量的值。 所以他们不能进入 $ _ POST </ code>数组。</ p>

我的问题是如何将这些值发送到PHP文件以及如何在PHP中访问这些值 文件?</ p>
</ div>

展开原文

原文

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个回答



你应该改变这个: formdata? formdata:form.serialize()</ code> </ p>

将其存储在变量中并连接要发送的值。</ p>

例如: </ p>

  var pars = formdata?  formdata:form.serialize(); 
pars + =“&amp; myField1 = myValue1&amp; myField2 = myValue2”
</ code> </ pre>
</ div>

展开原文

原文

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"

douba6361
douba6361 使用$ _POST [“myField1”],您可以访问myField1参数的值(在本例中为“myValue1”)。
接近 6 年之前 回复
drwghu6386
drwghu6386 这不是一个完整的解决方案。 我应该如何在PHP中访问这些数据? 你可以把它整合到我的代码中吗?
接近 6 年之前 回复



正如@chris所说,你需要做的就是将你自己的隐藏变量连接起来发布变量。 正如我所看到的,你对如何在php文件中使用这些额外变量感到困惑,这里有一个简单的例子:</ p>

  var params = formdata?  formdata:form.serialize(); 
params + =“param1 = myExtraVar1&amp; param2 = myExtraVar2”;
</ code> </ pre>

现在您已准备好发送所有变量 你的php文件,在ajax调用中修改你的数据参数,如下所示:</ p>

  ... data:params,
</ code> </ pre>

< 到目前为止,这么好。 让我们看另一边(PHP)</ p>

 &lt;?php 
//获取你想要处理的变量。
$ param1 = $ _POST ['param1'] ; //现在你可以从ajax调用中访问这个变量
//注意你可以通过使用var_dump($ _ POST)或print_r($ _ POST)
来转储你在超全局变量POST
//中显示的所有变量 </ code> </ pre>

希望这有助于更好地理解这个过程,并随时发表评论,我会回复你</ p>

另一件事我 捕获并且我想与您分享的是,您可以使用数据类型转换为JSON而不是转换返回的响应,因此您可以将此代码放在ajax调用中的任何位置:</ p>

  dataType:“json”,//如果你把它放在最后一行,省略逗号,否则保持原样
</ code> </ pre>
</ div>

展开原文

原文

As @chris said, all you need to do is to concatenate your own hidden variables to post variables. As I see, you are confused about how to use those extra variables in your php file, here's simple example:

var params = formdata ? formdata : form.serialize();
params += "param1=myExtraVar1&param2=myExtraVar2";

So now you have all variables ready to be sent to your php file, modify your data parameter in ajax call like this:

...data: params,

So far, so good. Let's see the other side (PHP)

<?php 
    // get the variables you want to treat.
    $param1 = $_POST['param1']; // now you have access to this variable from ajax call
   // Notice you can display all variables you have in superglobal variable POST 
   // by dumping it using either var_dump($_POST) or print_r($_POST)

Hope this helps understand better the process, and feel free to comment and I'll get back to you

Another thing I captured and I'd like to share with you is that you can use datatype to JSON instead of casting your returned response, so you can put this code anywhere inside your ajax call:

dataType: "json", // if you put this in last line, omit the comma, otherwise leave as it is

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐