-1

I need to submit data to server (using post with form) and catch a return status so that I can return a resolve or reject. I cannot use ajax to make the request neither XMLHttpRequest. I would like to know how can I submit form and return a promise (like @SomeKittens answer on 'the promise constructor'). custom.js :

var myfunction = 
{
   submitForm: function() 
   {
     // Create form
     var form = document.createElement('form');
     form.setAttribute('action', 'server.php');
     form.setAttribute('method', 'POST');

     var input1 = document.createElement('input');
     input1.setAttribute('type', 'hidden');
     input1.setAttribute('name', 'params');
     input1.setAttribute('id', 'params');
     input1.setAttribute('value', 'myvalue');

     form.appendChild(input1);

     // Create iframe and append form to it
     var $frame = $('<iframe style="width:500px;height:400px;display:none;">');
     $('body').html( $frame );
      setTimeout( function(){
         var doc = $frame[0].contentWindow.document;
         var $body = $('body',doc);
         $body.html(form);

         // submit form
         $(form).submit();}, 1 );
    }
 }

index.html :

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="custom.js"></script>
  </head>
  <body>
    <script>
      myfunction.submitForm().done(function () {
        alert('It works');
      }).fail(function () {
         alert('Failed');});
   </script>
  </body>
 </html>

Thanks

Community
  • 1
  • 1
John
  • 107
  • 1
  • 9
  • 1
    `I cannot use ajax to make the request neither XMLHttpRequest.` - firstly, if you can't use AjAX, then of course you can't use xhr, as xhr is how one does ajax to begin with!! and if you can't use ajax then you're basically left with using the standard form submit mechanism ... which leads to a new page being loaded ... so, Promises are completely out of the question too – Jaromanda X Dec 30 '16 at 09:40
  • @Jaromanda, I know it (I saw in that answer : http://stackoverflow.com/questions/8039402/where-is-jquery-ajax-defined-in-the-jquery-source-code). I have to use promise on return data (server will return data) and show 'it work' or 'it fail' based on status – John Dec 30 '16 at 09:57
  • Not sure how any answer in that question will help you to not use "AJAX or XHR" - because that question and answer is all about jQuery.ajax ... which is AJAX that uses XMLHttpRequest - which are specifically not "allowed" to be used in your question – Jaromanda X Dec 30 '16 at 10:09

1 Answers1

1

This cannot be done with javascript. A form submission creates a new request to the server, hence you will be directed to another page than the one you are within , in you case it is 'server.php' which in turn shall process the request and return the response just like any other page on the server. Its up to you where you go next from 'server.php'

In order to track the status of the server, you can return back from 'server.php' to the requesting page and either use query string within the returning URL or add your data to the server session and catch them when back on the page.

KAD
  • 10,972
  • 4
  • 31
  • 73
  • the server will return some data and I need to catch up (I don't know to do it in javascript as submit does not return anything) that data and show 'it work' (throught a promise) or 'Failed'. – John Dec 30 '16 at 09:53
  • Check this out : http://stackoverflow.com/questions/754761/read-session-value-with-javascript – KAD Dec 30 '16 at 10:02
  • `I don't know to do it in javascript as submit does not return anything` - you need to use AJAX, jQuerey.ajax returns a "promise" (of sorts), the underlying mechanism of jQueery.ajax is XMLHttpRequest ... oops, you aren't allowed to use that. Seems what you've asked for is impossible – Jaromanda X Dec 30 '16 at 10:11
  • @KAD, thanks for your help(I'm not allowed to use server-side code like php). – John Dec 30 '16 at 10:22
  • @Jaromanda, you're right I'm not allowed to use ajax mechanism. I will try another method.Thanks for your help – John Dec 30 '16 at 10:25