-3

Ajax parsing, wondering if it is possible if i post a form with text and images through an html form. And just getting the response back through Ajax?

Steps 1.) Form is in html and submit button in html

2.) Form submitted to php, and it runs through verifying/Uploading text and images.

3.) Any echos from the php is received through AJAX.

I am not sure if its possible to do so. Thanks for your time.

Javascript

$("button#submitbutton").click(function () {

    $.ajax({
        url: ("submitlisting.php"),
        type: 'GET',
        dataType: "text",
        success: function (data) {
            console.log(data); // e == result from the ajax call.
            $("p#result").html(data);
        }
    });
});
Benyaman
  • 451
  • 2
  • 10
  • 25
  • 1
    Of course it's possible. Submitting forms via Ajax is very common. – Felix Kling Jul 11 '14 at 04:09
  • its possible by using ajax request. – Varun Sridharan Jul 11 '14 at 04:10
  • But i am not submitting the form with Ajax tho? I am submitting through html, and just receiving the echo in Ajax. Because i have images to submit, and after i read this, http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload i am not sure how to allow images to be uploaded through Ajax. But i just need the response. – Benyaman Jul 11 '14 at 04:12
  • Uploading images/files with AJAX is not very well supported. Using iframes to upload images is kind of AJAX-y but not AJAX. You could try this: http://stackoverflow.com/questions/16495836/how-to-upload-an-image-by-ajax or this: http://www.akchauhan.com/upload-image-using-hidden-iframe/ – PitaJ Jul 11 '14 at 04:14
  • Hi Pita, i can already upload my images already just through html form and run through the php, but i just can't get the echos from php back into my html. Not sure how to do that. – Benyaman Jul 11 '14 at 04:15
  • The title does not do justice to the real problem of the question. – Protomen Jul 11 '14 at 04:17
  • @FelixKling , can you plese answer the question? – Pratik Joshi Jul 11 '14 at 04:22
  • @PratikJoshi: I don't see much sense in answering such questions. Now that it seems to be changed (or rather clarified) the answer is: No, it's not possible. Feel free to post that as an answer if you want to. – Felix Kling Jul 11 '14 at 04:27
  • You can upload files with Ajax if the browser supports XHR2: http://www.html5rocks.com/en/tutorials/file/xhr2/ . Whether that is an option for you or not can only you decide. – Felix Kling Jul 11 '14 at 04:29

2 Answers2

1

use a hidden iframe as target for the form

<form target="form_result"><!-- input elements --> </form>
<iframe name="form_result" style="display:none;"></iframe>

Then after form is submitted, check the content of the iframe to read the response.

gp.
  • 8,074
  • 3
  • 38
  • 39
1

No. Not in the way you're thinking. The server responds to a request and the cycle ends there. If you submit a form, that is one request, and the client cannot listen to arbitrary requests sent by the server. To obtain the functionality that you are thinking of (seeing some sort of progress as the user is uploading a file, for example), you would need to do everything by AJAX.

A simplistic way is to use some sort of AJAX file uploader (blueimp comes to mind: https://github.com/blueimp/jQuery-File-Upload), and then poll the server for progress updates using AJAX.

sahbeewah
  • 2,690
  • 1
  • 12
  • 18