0

I am trying to build off a question I asked yesterday.

I am able to pass the file over to PHP using the ajax method. But I need to be able to change the file name to the booking number. For some reason, the booking was not being passed over to the PHP script. So I attempted the following:

$('#uploadBtn').on('click', function()
{
  var form_data = new FormData();
  form_data.append("file", document.getElementById('pdfFile').files[0]);
  var booking = $('#bookingNum').val();
  var partner = $('#partnerCode').val();

  $.post('process/fileUpload.php', {booking:booking, partner:partner}, function(data)
  {
    // Wasn't sure if I needed anything here
    console.log(data);
  });

  $.ajax({
    url: 'process/fileUpload.php',
    method:"POST",
    data: form_data,
    contentType: false,
    cache: false,
    processData: false,
    success: function(data){console.log(data);},
    error: function(jqHHR, textStatus, errorThrown){console.log('fail: ' + errorThrown);}     
  });
});

As you will notice above, I had to use the $.post method to send the booking and partner over to the php script.

I then used $.ajax to send the form_data over to the same script.

(I could not achieve this in one motion from yesterday's question I asked. So this is my second attempt to complete this. If there is a way to send all of the info in one motion, please refer to the question I linked above.)

So over in the PHP script, I am able to get all of the items I needed with a couple of functions:

<?php
  // from the $.post method
  if(isset($_POST['booking']))
  {
    $booking = $_POST['booking'];
    $partner = $_POST['partner'];
    getInfo($booking);
  }
  // from the $.ajax method
  if($_FILES['file'])
  {
    $file = var_dump($_FILES['file']);
    getFile($file);
  }

  function getInfo($booking)
  {
    return $booking;
  }

  function getFile($file)
  {
    return $file;
  }
?>

I know it's not pretty, but I am able to get the booking (I don't need the partner right now), and I am also able to get the file information.

What I need to do is rename the file to the booking, and then finally upload it to the necessary directory.

I wasn't sure if I had to combine the functions, but I did try to no avail.

With that said, I am able to get the booking and file info within the PHP script. Now how would I go about renaming the file to the booking?

2
  • 2
    Don't do this - fix the issue from the last question. Solve your issue by sending two requests is totally the wrong way! Commented Jun 22, 2018 at 16:54
  • @Phillipp - Will do. I have abandoned it already. Commented Jun 22, 2018 at 17:14

2 Answers 2

2

As you used form_data.append() to add the file data to the formdata. did it not occur to you to also use that to add the booking and partner values to it as well?

$('#uploadBtn').on('click', function()
{
    var form_data = new FormData();
    form_data.append("file", document.getElementById('pdfFile').files[0]);
    form_data.append('booking', $('#bookingNum').val());
    form_data.append('partner', $('#partnerCode').val());

    $.post('process/fileUpload.php', form_data, function(data)
        {
            console.log(data);
        });
});
Sign up to request clarification or add additional context in comments.

6 Comments

I cannot use $.post method in this case, as I get an "illegal invocation" error.
And when I use the $.ajax method, I only get the file info.
Thats because you need to re-jig your PHP code to work when all the data arrives in one post
Oh and you should use echo json_encode($something); to return data to the AJAX call
It would look like any other form processing code in PHP, millions of examples out there
|
1

To fix your ajax request (especially the illegal invocation), use the following javascript code

$('#uploadBtn').on('click', function()
{
    var form_data = new FormData();
    form_data.append("file", document.getElementById('pdfFile').files[0]);
    form_data.append('booking', $('#bookingNum').val());
    form_data.append('partner', $('#partnerCode').val());

    $.ajax({
        type: "POST",
        url: 'process/fileUpload.php',
        data: form_data,
        processData: false,
        contentType: false,
        success: function(data) { console.log(data); }
    });
});

Notice the use of processData: false and contentType: false

11 Comments

The problem I'm having is over on the PHP side. I can echo the file info, but I cannot get the booking. Thoughts?
Come on, now. I don't want anyone to do my work. Everything I have attempted has failed. As stated, I can echo the file info on the PHP side, but I cannot seem to echo the booking. How is that traits of a "help-vampire"?
@Philipp - I just edited your answer with the revised PHP code I attempted.
It seems, you don't - just tested it and the example above works perfectly. I $_POST['booking'] is empty, it was empty at the point you sended the request..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.