0

I have a page where I would like a form to be completed , a javascript file verifies the information, sends the information to a php file to mail it to me and then initiates a download. The problem is that I can have the javascript file either send the mail or allow the file to be downloaded, but not both.

The code in question looks like this:

else {
document.dlform7.action = "http://www.myurl.com/c/post7.php" ;
document.dlform7.submit();
window.location.assign(url);
}

Is there a way (perhaps using load) that I can have both of these actions take place at once?

edit:

ajax option:

 else {
 $.ajax({
    url: "http://www.myurl.com/c/post7.php";
    type: "POST",
    data: data,     
    cache: false,
    success: function () {              
        window.location.assign(url);
        document.dlform7.submit();                   

        },  
Patrick
  • 53
  • 8
  • 3
    Submit the form to a php script that first [sends the email](http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php), and then [downloads the file](http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php) – adeneo May 19 '16 at 15:24

1 Answers1

2

I think that you need to adjust the submit handler for your form. IRC, the submit event will cause the page to reload.

You will want to create an submit event handler for the form that will make an ajax call to your php script. Then have it return false to prevent the page reload. You should then be able to cause the file download at the same time.

UPDATE

If you want to download the file no matter what happens with the php script to send the email you can do the following:

else {
 $.ajax({
    url: "http://www.myurl.com/c/post7.php";
    type: "POST",
    data: $(document.dlform7).serialize(),     
    cache: false,
  });
  window.location.assign(url);

The ajax method is the form submission. It doesn't break the flow of your code so the redirect would happen after the request. (I am pretty sure that this will work).

If you want to wait until the php script successfully runs then you would move the code into the success callback like so:

else {
 $.ajax({
    url: "http://www.myurl.com/c/post7.php",
    type: "POST",
    data: $(document.dlform7).serialize(),     
    cache: false,
    success: function () { window.location.assign(url); }
  });

Of course, you could also just change the php page to send an email and then respond with the file that you want to download as mention in the comments.

Schleis
  • 41,516
  • 7
  • 68
  • 87
  • I submit using onClick="return checkValuesControl(true);" – Patrick May 19 '16 at 15:45
  • From the code snippet that you posted, the line `document.dlform7.submit();` causes the page to reload which causes the code to not go to the next line. Change this line so that it calls an asynchronous submit (make an ajax call). And you should be able to do everything that you are looking for. – Schleis May 19 '16 at 15:49
  • So would I put the downloading of the file in the success function of the ajax call? – Patrick May 19 '16 at 16:09
  • That would depend on what it is that you want to do. If you want the file download only to occur if the mail was successful, then yes. If the file should be downloaded regardless, then have it after the ajax call function. – Schleis May 19 '16 at 16:27
  • I added edits to the original showing the ajax code. Is this correct? Am I missing sometimes there? – Patrick May 19 '16 at 17:38
  • Thank you for the edits. Just a small note for anyone looking at this in the future, the first line should have a comma at the end and not a semi-colon. url: "http://www.myurl.com/c/post7.php", – Patrick May 20 '16 at 15:03