1

I'm trying to pass 5 strings and some uploaded images as FormData with AJAX to a php script, the same php-file as the one that executes the ajax code (isolated by if($mode="edit"){ ... });

php file containing all the code: ?page=shop&mode=edit

ajax code:

$.ajax({
        url: "?page=shop&mode=edit",
        type: "POST",
        dataType: 'multipart/form-data',
        data:  formData,
        contentType: false,
        cache: false,
        processData: false,
        success: function(phpfeedback) { console.log('success: '+phpfeedback); },
        error: function(phpfeedback) { console.log('error: '+phpfeedback); }
    });

php code:

if($mode=='edit'){

    //php code that logs all feedback into $ajax_feedback
    //I skip the phpcode as it is very long but it should be correct

    echo $ajax_feedback;
}

when I execute the ajax code I get: error: [object Object] in the console.

Why don't I see the $ajax_feedback string? Please ask me if you need any more information, I really searched for hours on this.

5
  • Your dataType is to specify the return format, not the format of data you are sending to the PHP code. You could set it to something like JSON, and json_encode your data before returning it, especially if you might return more than one value. Right now, Ajax is expecting the return to be in "multipart/form-data" format, which you can't echo. I'm not even sure it will support returned data in that format, so maybe it's just confused. Commented Oct 26, 2020 at 19:17
  • What is this "?page=shop&mode=edit" url returning ? is it json? Commented Oct 26, 2020 at 19:29
  • Use console.log('error: ', phpfeedback); instead Commented Oct 26, 2020 at 19:40
  • @droopsnoot I changed dataType to 'html' and now it works I did not know it needed to be the return format! many thanks, it works now. Commented Oct 26, 2020 at 19:40
  • just now I got the full html document returned (the html output of the php file). I figured out now this is normal. But how can I let the (isolated) php code: if($mode=="edit"){ ... } return only the content of the string: $ajax_feedback ? And not the whole html document. Commented Oct 26, 2020 at 22:40

1 Answer 1

1

Thanks to a great comment I figured out that dataType is to specify the return format, not the format of data you are sending to the PHP code. As I wanted to return the content of a text string containing the error info of the php script I had to change the dataType from multipart/form-data to text.

$.ajax({
        dataType: 'text',
 });

Now I get a whole html document back from php (which I figured out is normal) but I would only want to have the content of one php string variable containing the error info back ($ajax_feedback). But i guess that is a new question.

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

4 Comments

Excellent, glad it pointed you the right way. Because this is the internet, I have to point out that if you just have a look at the documentation, it's all covered in there: api.jquery.com/Jquery.ajax
As for only returning some parts, you need to make your PHP code only output what you need. Ajax always captures all the output from the server-side code. You could always separate your "edit" code into a separate PHP file, call that directly from your Ajax, but require_once() it in the original, larger PHP file that it is currently in.
I see, I tried that before and I know that works, but in my complicated site structure I would prefer if there was a way, any way, to just pass a string from an ajax call to the mother script. I tried with these: ob_start(); ob_get_contents(); and ob_end_clean(); but to hard for me. I also tried with $_SESSION['ajax_feedback']; but that did't work either. Any other hints? I feel you know more than me :)
When you call some PHP (or any other server-side language) using Ajax, all the output of that server-side script is captured and returned to the calling Ajax function. So the only way to only get one item back is to only send one item back. You could send everything back, and then parse the returned data for the information you need, but that's a really messy way of doing it, and very wasteful on resources. I wouldn't like to say I know more than you, I'm just learning myself. I believe you can use session variables, though, so you could look into that.

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.