Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to use a document object to send a for using AJAX

    Hi All,
    I have a website that is used assess examination responses from students. When I first created the code I used the document object of the browser to hold the form so that it was not visible and I could programatically populate all the fields. When the document was ready for sending then I would use the submit method document object to transfer the form contents to my PHP mail program which then send it on to me.
    This all worked well but I didn't want the sending to open a window on the student browser, I would like this to be done transparently.

    The coding to create the object looked like this:

    Code:
    var ResultForm = '<html><body><form name="Results" id="submit" action="http://glenavon.com/FormToEmail.php" method="post">';
    ResultForm += '<input type="hidden" name="recipient" value="[email protected]"></input>';
    ResultForm += '<input type="hidden" name="subject" value="TEST QUESTIONS"></input>';
    ResultForm += '<input type="hidden" name="Exercise" value="PAPER 1"></input>';
    ResultForm += '<input type="hidden" name="realname" value=""></input>';
    ResultForm += '<input type="hidden" name="Score" value=""></input>';
    ResultForm += '<input type="hidden" name="Start_Time" value=""></input>';
    ResultForm += '<input type="hidden" name="End_Time" value=""></input>';
    ResultForm += '<textarea style="visibility:hidden;position:absolute;" name="Stats">';
    
    
    ResultForm += '</form>';
    ResultForm += '</body>';
    ResultForm += '</html>';
    When the object is fully populated, the submit meth is called:

    Code:
    NewWin.document.Results.Stats.value = message;
    But, of course this opens a browser window. I did get some help on this forum that said I should use AJAX. Ideally I would like to have used the object that I had already created, but, after lots of different tries I didn't succeed in getting the form contents over to my PHP file. But I did get the following to work:

    Code:
    <script>
    $(document).ready(function(){
    
      $('#submitA').click(function(){
         $.post("http://glenavon.online/send.php", $("#submit").serialize(),  function(response) {   
           $('#success').html(response);
         });
       return false;
      });
    
    });
    
    </script>
    <form action="" method="post" id="mycontactform" >
    <input type="text" name="realname" id="realname" /><br />
    <input type="text" name="recipient" id="recipient" /><br />
    <textarea name="Stats" id="Stats"></textarea><br />
    <input type="button" value="send" id="submitA" /><div id="success" style="color:red;"></div>
    </form>
    I then use a trigger method to generate the click event and also comment out the original submit:

    Code:
    	$( "#submitA" ).trigger( "click" );
    	//NewWin.document.Results.submit();
    This works fine but I would like to use the document object created for the previous submit method, if possible. I could use this new webform and populate it using innerHTML (I would also need to hide the fields of course.

    But I would just like to be able to work with an object and then send this.

    Any help would be gratefully received.

    Thganks

    Jonel

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,823
    Thanks
    26
    Thanked 606 Times in 599 Posts
    Question: are all the form data populated programmatically or is at some stage user interaction (despite pressing a submit button) required?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. #3
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All the data are entered programmatically. When the user is satisfied that they have finished or if their time is up then the form is submitted. So the only users interaction is to click on 'Finish', but this is independent of the form itself.
    What I really want is that the user should click on finish or a timeout occurs and the form data is sent without any browser windows opening.

  4. #4
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,823
    Thanks
    26
    Thanked 606 Times in 599 Posts
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •