0

I have the below that uploads an image using JQuery's AJAX function

var id = <?php echo $id; ?>;
var img_data = new FormData($("form")[0]);

$.ajax({
    url: 'add.php',
    data: img_data,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
        alert(data);
    }
});

I'd like to include a string with the FormData sent. I tried the following, but no luck

data: img_data {id: id},

What's the correct syntax here?

1
  • store the id in a hidden input in the form. Commented Sep 3, 2013 at 21:28

2 Answers 2

4

Use append

var img_data = new FormData($("form")[0]);
img_data.append('id', id); 
Sign up to request clarification or add additional context in comments.

Comments

-1

You could create a JSON data object and pass that as application/json and process the data within add.php:

var data = {
    id : <?php echo !empty($id) ? $id : "''",
    img_data : new FormData($("form")[0])
};

$.ajax({
    url: 'add.php',
    data: data,
    contentType: "application/json",
    type: 'POST',
    success: function(data){
        alert(data);
    }
});

Although unconventional, you could also append the data as a query string to the URL with the POST. You'd also need to edit add.php to get this parameter.

$.ajax({
    url: 'add.php?id=' + id,
    data: img_data,
    processData: false,
    type: 'POST',
    success: function(data){
        alert(data);
    }
});

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.