-3

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
 
    header("Location: final.html");
}
?>

I have a form that when completed executes a script, but I don't want that script to show in the url part of the browser, so I want it to either run in background or instantly close even if the script wasn't completely done.

How can I achieve this?

Baum mit Augen
  • 46,177
  • 22
  • 136
  • 173
Rafael De Sá
  • 53
  • 1
  • 10
  • 1
    You can try to use Ajax for that – NullDev Apr 05 '17 at 14:33
  • how? I never used Ajax – Rafael De Sá Apr 05 '17 at 14:34
  • 1
    Post the form to the processing script > process > `header()` redirect to another page; the processing script URL won't appear in the address bar nor the browser's cache. – CD001 Apr 05 '17 at 14:34
  • @RafaelDeSá We / I can't help you if you do not provide any code... Please edit your question with the script you have so far. – NullDev Apr 05 '17 at 14:36
  • Sounds like you're using `method="GET"` (or no method at all). POST fixes that issue. Or it might be that you're sending the form to be processed in a different file (it can be in the same). Usage of AJAX is also a solution here. – Qirel Apr 05 '17 at 14:47
  • im using post method, i wanted to use ajax because everyone says to, but i never worked with ajax so i dont know how to do it – Rafael De Sá Apr 05 '17 at 14:48
  • i´ve posted a little bit of the code and when it redirects to a new header the php script should run in the background and not open a new blank page – Rafael De Sá Apr 05 '17 at 14:50

2 Answers2

0

You need Javascript Ajax. A best Practice to run PHP in Background is to use Ajax.

A good Practice is:

HTML -> JS/Ajax -> PHP HTML <- JS/Ajax <- PHP

rob1337
  • 41
  • 4
  • how can i do it, i never used ajax – Rafael De Sá Apr 05 '17 at 14:44
  • That was answered so many times on Stackoverflow, here the classic one: [link]http://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest[/link]. With jQuery is a lot easier, like this: [link]http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php[/link] – rob1337 Apr 05 '17 at 15:25
0

You can achieve this by using ajax. For example once a person is done filling in a form, they can press a button. Then you set up a click listener for this button and execute the ajax call. The ajax method will send data to the php script and the php script will run, then return whatever you set it to return to the ajax method without reloading the page and you will be able to manipulate the result in whatever way you please. For example,

Your form submit button;

<button id="submit">Submit Form</button>

Your javascript listener and ajax method;

//This is executed whenever the submit buttom is clicked.
$( "#submit" ).bind( "click", function() {

      var myVar="Test Variable";

   $.ajax({
         type: 'POST',
           url: "get_message.php",//your php script
           data: {myVar:myVar},//your variable
           success: function(result){
              alert(result);
           }
     })
})

Your php script which i called get_message.php

<?php
$var = $_POST['myVar'];//get sent variable from ajax using post method
echo "The sent string is " . $var;

?>

This should then output The sent string is Test Variable.

Let me know if this answer helps you or you have any problems. Please note that i you have to include jquery for the code to work.

Douglas Hosea
  • 826
  • 9
  • 26