• 0

Execute PHP script in the background


Question

I'm pretty new to PHP so please just bear with me here...

I'm wondering if it's possible to execute a PHP script in the background, without disrupting the user's experience.

I've created a contact form and specified contact.php as it's action. However when submitted, this navigates the user away from the page and I use the PHP echo function to return the user back to the page once the script has completed (once the message has sent). What I'm trying to do specifically is this; once the user clicks "send", contact.php will execute in the background and once it has completed (or once the message has sent), a DIV will be displayed on the page - and all this occurs without the visitor being taken away from that page and without any other windows appearing.

Is it possible?

Link to comment
https://www.neowin.net/forum/topic/617980-execute-php-script-in-the-background/
Share on other sites

8 answers to this question

Recommended Posts

  • 0

What you want is AJAX.

http://www.w3schools.com/php/php_ajax_intro.asp

  Quote
The AJAX technique makes web pages more responsive by exchanging data with the web server behind the scenes, instead of reloading an entire web page each time a user makes a change.
  • 0

What you want to accomplish will require AJAX functionality, which is really just JavaScript communicating with a server. There are several JS frameworks that have built in AJAX functionality that makes the whole process much, much easier to do. I'd recommend jQuery, but there are other good frameworks out there.

You want to stop the form from submitting in the browser, or make your submit a button so that it doesn't post. I don't have time atm, but there's a little bit more to explain, like returning your data in JSON format and easily using that in JS, among other things. Just get started and see where you go, there are a lot of examples online :)

  • 0

I would also recommend jQuery. Once it's referenced within your HTML file, your Javascript can be as simple as this to do what you've requested:

$.ajax({type:"GET", url:"sendform.php?var1=text&var2=text", success: function(text) 
  {
	// It was a success, do something here
	$("#ThankYou").show();
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) 
  {
	// There was an error, scream
	$("#Error").show();
  }
});

  • 0
  Rob said:
I would also recommend jQuery. Once it's referenced within your HTML file, your Javascript can be as simple as this to do what you've requested:

$.ajax({type:"GET", url:"sendform.php?var1=text&var2=text", success: function(text) 
  {
	// It was a success, do something here
	$("#ThankYou").show();
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) 
  {
	// There was an error, scream
	$("#Error").show();
  }
});

So let me get this straight (I'm away from my computer so I can't test this myself). If I download jQuery and link it to my page, where would I insert that code?
  • 0

You'd have, somewhere, a button with an ID of Submit, let's say. Then you could place the following in a Script tag:

$(document).ready(function(){
  $("#Submit").click(function(){
	$.ajax({type:"GET", url:"test.txt", success: function(text){
		// The contents of test.txt have successfully been received through AJAX, do something here
	  },error: function(XMLHttpRequest, textStatus, errorThrown){
		// There's been an error, do something here
	  }
	});
  });
});

This, of course, is an example and doesn't actually submit your form, but just showcases how AJAX works with jQuery - I'd suggest looking at some jQuery tutorials as it's important you learn the basic syntax.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Plenty of info (benchmarks etc) out there about how turning off e-cores can increase performance in certain workloads. Which tells me that Microsoft or Intel can’t get this mess of cores sorted out. At work we have this lab and someone was testing (TrueNas performance testing) ESXi on a white box setup that had a 13700K in it and ESXi would purple screen every time until they turned off ecores.
    • I've been using OneDrive for photos and documents since Windows 8.1 came out and it's been the best way to keep a backup of what I have. I also use GoogleDrive on my phone with Android 15 to back up my phone. Over the past 12 years, I've seen people losing data on their PC or phone because they don't use cloud services. Even though I warn them to use them. Especially those who make money working on their PC with thousands of files. A 1TB cloud service is extremely cheap these days and there's no reason for you not to use one. I've seen people losing valuable data on their phone because they don't use GoogleDrive on their phone and this is much more common than on a PC. On Windows, I've seen people having hardware problems with their HD and SSD and losing all their data just because they refused to use OneDrive, which offered the backup option. There's still an external SSD as a second copy to be used together with your OneDrive. Bitlocker is completely safe, you will only have problems with it if you use bad programs that mess with the integrity of Windows. Your iPhone and Android also have encryption and if you mess with the system gears it can bug and you can lose your data too. Windows 11 is perfect if used correctly. But people are stupid enough to buy a brand new car, open the engine and start messing with it until it stops working, and then go to the car manufacturer to complain that it stopped working just because the user messed up the engine.
    • Hey it's not my fault a politician's team made a tech blunder; a significant one at that, is it?
  • Recent Achievements

    • One Month Later
      mevinyavin earned a badge
      One Month Later
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
    • Veteran
      matthiew went up a rank
      Veteran
    • Enthusiast
      Motoman26 went up a rank
      Enthusiast
  • Popular Contributors

    1. 1
      +primortal
      684
    2. 2
      ATLien_0
      266
    3. 3
      Michael Scrip
      196
    4. 4
      +FloatingFatMan
      181
    5. 5
      Steven P.
      140
  • Tell a friend

    Love Neowin? Tell a friend!