Jump to content

transferring variables to a page that's not loaded into the browser


ijp

Recommended Posts

I have a form that collects information such as name, email address etc that passes these variables to another php page that sends out a confirmation email to the person filling out the form as well as a copy to us. Normally the code to send the emails would be on the same page that processes the form and it works perfectly that way transferring the variables to the email. In this instance I have used the following format for the first name variable..

 

Dear <?php echo $_POST["firstname"]; ?>

 

As I said this works well. The problem is that I needed to put a delay on the email before it's sent out so I took the code that sends the email and placed it in a new page that then gets called once the "thank you" page after the form is successfully submitted loads. I have used the following code on the thank you page to achieve this..

 

exec ("/usr/local/bin/php emailsend.php >/dev/null &#038;");

 

The file "emailsend.php" has the email processing code and the delay. This way the program runs in the background and doesn't delay the loading of the thank you page in the browser.  Once it finishes running the emails are then sent out.

 

It all works well except that now the variables aren't being transferred to the email. I have tried sessions with no luck.  Is there a way to keep all of this on the one page so that the thank you page doesn't have to wait until after the delay before it loads which is the reason I separated the code into 2 files? Is there a better way of setting and retrieving the session variables in this case?

Link to comment
Share on other sites

Hello,

 

There are several options in how I see it.

 

1) If you want to run a shell command, than your best bet would be to first store the data in a table and then access that data through the PHP shell script.

 

2) Another option that I prefer myself, would be to use curl. But then you will need to wait for the curl request to process (as far as I know). There may be a way of having the curl POST message sent and have it not wait for a response, but I have never tried this before.

Link to comment
Share on other sites

Hi,

 

Im not 100% on this but if you run exec ("/usr/local/bin/php emailsend.php >/dev/null &#038;"); the script will still wait for the command to finish.

 

If speed is a factor I would store the data in a database and have a cron job that runs the emailsend.php script every 5 mins or so that goes through the database and removes the entries as it sends the emails.

 

This would ensure that your users dont have to wait for the email to be sent and getting the data is a simple mysql query. Plus if the script crashes the data still waiting to be send will be stored in the database.

 

 

Link to comment
Share on other sites

Thanks for your suggestions..I really appreciate your help.

 

Hi ijp,

You could use PHP's sleep() function to delay the execution of the email delivery function and keep all the code on the same page. However, do you want to display a thank you message when you do not know whether or not the emails have actually been sent?

Fergal

 

The problem I found with the sleep function on the same page was that it also delayed the thank you page from loading during the delay so that's why I separated the code into 2 pages.

 

Hi,

 

Im not 100% on this but if you run exec ("/usr/local/bin/php emailsend.php >/dev/null &#038;"); the script will still wait for the command to finish.

 

If speed is a factor I would store the data in a database and have a cron job that runs the emailsend.php script every 5 mins or so that goes through the database and removes the entries as it sends the emails.

 

This would ensure that your users dont have to wait for the email to be sent and getting the data is a simple mysql query. Plus if the script crashes the data still waiting to be send will be stored in the database.

 

 

 

I tested out a cron job but that too didn't have the variables in the emails so the problem is just accessing the variables. They appear when the email code is on the same page as the form processing but they don't appear when I put the email sending code onto another page and try to use sessions to access them.

 

Hello,

 

There are several options in how I see it.

 

1) If you want to run a shell command, than your best bet would be to first store the data in a table and then access that data through the PHP shell script.

 

2) Another option that I prefer myself, would be to use curl. But then you will need to wait for the curl request to process (as far as I know). There may be a way of having the curl POST message sent and have it not wait for a response, but I have never tried this before.

 

chrisroane can you give me an example of how I can use the shell script to call up the data from a table. My coding skills are not as advanced as I'd like but that's one thing I haven't tried yet.

Link to comment
Share on other sites

Basically all you would do is the following:

 

1. Before you run the exec() command you will want to insert data into a database table. I usually use MySQL, but you could use anything, including a text file for this.  If you do not know how to insert data into a mysql table, look for tutorials on using php/mysql on Google. Unless you get someone to write the whole thing for you, you will probably need to understand the basics in working with a mysql table in php.

 

2. Now on the php shell script (emailsend.php), you will simply select the row that you just added in the other script. I am not sure if you can send get variables to a php shell script or not, but it would be easy to test (so instead of emailsend.php it would be emailsend.php?var1=value1). You would want to pass the primary key value from your main script to emailsend.php so that you could select the correct row.

 

 

Doing it this way would display the thank you page before the exec() command finished, which would make sense if it takes your server a while to send emails. You could also use this same technique to run a cron job that processes emailsend.php (like what d_barszczak suggested).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.