How can I send values โ€‹โ€‹from jQuery-AJAX function to PHP file and access those values โ€‹โ€‹in PHP file?

I wrote a jQuery-AJAX function like this:

$('#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 can access the values โ€‹โ€‹being filled in by the user in the form using an array $_POST

in the PHP file. But I also want to pass the values โ€‹โ€‹that I put in the comments in my code above to a PHP file.

The values โ€‹โ€‹/ parameters I want to submit are not part of the form fields. I have manipulated the values โ€‹โ€‹of these variables. Therefore, they cannot enter the array $_POST

.

My problem is how to send these values โ€‹โ€‹to the PHP file and how do I access these values โ€‹โ€‹in the PHP file?

+3


source to share


2 answers


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

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



Example:

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

      

0


source


As @chris said, all you have to do is merge your own hidden variables into post variables. As I see you are confused about how to use these extra variables in your php file, here is a simple example:

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

      

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

...data: params,

      

So far so good. Let's look at 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 to better understand the process and feel free to comment and I'll get back to you.

Another thing I have captured, and I would like to share with you, is that you can use the datatype for JSON instead of discarding the returned response, so you can put this code anywhere in your ajax call:

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

      

0


source







All Articles