• 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
Share on other sites

8 answers to this question

Recommended Posts

  • 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 :)

Link to comment
Share on other sites

  • 0

Ah okay, thanks for that guys. I'll look into AJAX then - I've never played around with it before. Any general help in this area won't go unappreciated though. (Y)

Link to comment
Share on other sites

  • 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();
  }
});

Link to comment
Share on other sites

  • 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();
  }
});

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?
Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

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

    • No registered users viewing this page.