1

UPDATE: SOLVED! (Answer below) I am trying to add vars with an image file I am uploading. I am able to upload the image fine but I can't seem to send the wanted vars to php and send them back etc.

I am hoping to send at least this var called $Destination to php, along with image data, which is working, and maybe other vars too. Below is the line I am trying to send.

var Dest = '<? echo $Destination;?>';

Here is my script:

 function myFunction() {
    var = Dest: '<? echo $Destination;?>'; // This one
    var file_data = $('#ImageuploadBtn').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);

    $.ajax({
                url: 'admin/myUpload.php',
                dataType: 'text',  
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'POST',
                success: function(php_script_response)
                { 
                    console.log(php_script_response);
                    var html = '<img src="../files/' + php_script_response + '" class="DisplayMainImage img-responsive">';

                    $( ".MainImage" ).replaceWith(html);
                }
     });

My PHP:

<?php
    if ( 0 < $_FILES['file']['error'] ) {
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {
        move_uploaded_file($_FILES['file']['tmp_name'], '../files/' . $_FILES['file']['name']);
        $Message = $_FILES['file']['name'];
        //// $Destination DOES NOT WORK
        $Destination = $_POST['Dest']; /// NO WORK
        echo $Message;

    }
?>

SOLVED ISSUE: I needed these lines.

var Dest = '<? echo $Destination;?>';
    form_data.append('Dest', Dest); 
5
  • 2
    It's a little confusing, you're not sending anything other than the image, did you at least try to form_data.append('Dest', Dest); to add the data? Commented Oct 27, 2016 at 15:26
  • That did it. Oh man. Thank you. Commented Oct 27, 2016 at 15:29
  • 2
    Actually, @adeneo should have been the one to have posted an answer, given the timestamps here. I think it's only fair. Commented Oct 27, 2016 at 15:34
  • Yes, @adeneo, please pose your answer and I will give you credit. That's what did it for me. Commented Oct 27, 2016 at 15:52
  • Marc B seems to have the right idea, just accept that one Commented Oct 27, 2016 at 16:35

3 Answers 3

3

This is a syntax error:

var = Dest: '<? echo $Destination;?>'; // This one
          ^---

and that will kill the entire <script> block, keeping your ajax call from ever occuring.

And even if that variable assignment worked, you don't ever USE it in your ajax code. You have a formdata, but the only thing you put in there is that one file item.

Sign up to request clarification or add additional context in comments.

Comments

1

First off all

var = Dest: '<? echo $Destination;?>';

will render to something like this:

var = Dest: 'some text from $Destination php var';

which is not correct js syntax. Since it's syntax error, script execution will stop on this moment. Maybe you meant something like this:

var dest = '<? echo $Destination;?>';

You can open developer tools in browser and see on js console rendered source. It will show you script errors. For example in chrome, you press ctrl + shift + i and go to console tab.

Comments

0

This was needed.

var Dest = '<? echo $Destination;?>';
form_data.append('Dest', Dest); 

Comments

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.