• 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

    • A few things I am wondering about for S3: A.  What's Pike doo gonna look like this time around....yea I've watched the trailers & it looks poofed up, but still.... B. When will the unintelligible, physco-babbling Pelia go away (or at least learn how to speak English properly, without that crappy, slurred accent that sounds like she's on crack, Mary Jane, & LSD at the same time) ?  Hopefully Scotty's arrival will mean her departure is near.... C.  When do we get to see more of the stunningly gorgeous No. 1, preferably in regular/civilian clothes that show off her physical attributes better ? D. Is Spock EVER gonna get laid properly, human style ?  I feel certain that Christine could make any Vulcan-human horizontal bomp a thing to remember for a LONG time  E.  Can we PLEEEEEEZE get rid of the hatchet-head/buzz cut hair styles on Ortega & anyone else that has it....  But otherwise, as Pike says:  HIT IT !  
    • RIP Hotlips..... IMHO, her best scenes were the few where she dared to let her REAL feminine side show through from underneath all that crappy, worn-torn soldier facade that she had to keep up with...she instantly lit up every room or situation where she was featured !
    • Helium Converter 3.3.69.0 by Razvan Serea Helium Converter is a free Windows utility for converting audio files between formats such as MP3, FLAC, AAC, WMA, OGG, and WAV. It supports batch conversion, preserves or updates tag information, and offers features like volume normalization. With a simple interface, it's ideal for users who need to convert large music libraries quickly and efficiently while retaining metadata. Helium Converter key features: Supports file formats: MP3, MP4, FLAC, AAC, M4A, WMA, WAV, OGG, OPUS, APE.... Batch conversion for large music libraries Preserves and edits metadata (ID3, Vorbis Comments, etc.) Volume normalization to equalize loudness Album art extraction and embedding Drag-and-drop interface for quick file selection Adjustable encoding parameters (bitrate, sample rate, channels) Uses internal codecs for consistent performance Supports CUE sheets for split track conversion File renaming based on tags during export Unicode support for international file and tag names Logging of conversion processes for troubleshooting Multi-core CPU support for faster conversions Download: Helium Converter 3.3.69.0 | 25.1 MB (Freeware) Links: Helium Converter Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Since it's been quite a while since the last episode aired, would it be fair (or cruel) to refer to Peggy as a MILF, or even a SMILF ?  HAHAHAHAHAHA
    • Their computers are not gonna stop working in October
  • Recent Achievements

    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      172
    2. 2
      ATLien_0
      122
    3. 3
      snowy owl
      121
    4. 4
      Xenon
      117
    5. 5
      +Edouard
      93
  • Tell a friend

    Love Neowin? Tell a friend!