How to make php wait for an confirm box to finish

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

tomarseneault
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Mar 31, 2014 2:09 am

How to make php wait for an confirm box to finish

Postby tomarseneault » Mon Mar 31, 2014 2:28 am

newbie question:

I have the following code:

echo "<script type=\"text/javascript\">var r=confirm(\"Do you really want to Reset?\"); if (!r) {window.location='/index.html';};</script>";
shell_exec('rm /tmp/*.x10');
shell_exec('/usr/bin/br --port=/dev/ttyUSB0 e all_off');

And I want to wait until the user answers the confirm box before I execute the two shell_exec statements, if the user does not want to reset he go back to index.html and does'nt run the shell_exec's. As it sits now the shell_exec's run before I get the confirm box. I have tried to put a sleep statement after the echo, but all three statements wait until the sleep is done and I get the same thing. I have tried buffering with ob_start() and ob_flush() but that was no help.

I know that javascript and php are running different machines so the echo and shell_exec's have no relation to each other as far as execution goes but I need to have the user confirm their request. At first I was trying to capture the output of the javascript (with document.write(r)) but I could not get it into a variable to test in a php if statement. This is frustrating, I can't be the first to want to do this. Anyone know how this could be done? Even if it's completely different than what I'm trying.

Any help would be appreciated.

Tom

raphael75
New php-forum User
New php-forum User
Posts: 20
Joined: Fri Apr 06, 2012 9:54 pm

Re: How to make php wait for an confirm box to finish

Postby raphael75 » Mon Mar 31, 2014 10:48 am

One approach would be to have 2 files like this:

confirm.html
contains the javascript asking if the user wants to reset.

Code: Select all

<script>
function check_reset()
{
  if(confirm('are you sure you want to reset?'))
  {
    window.location = 'reset.php'
  }
  else
  {
    window.location = 'index.php'
  }
}
</script>


Then reset.php would be like this:

Code: Select all

<?php
shell_exec('rm /tmp/*.x10');
shell_exec('/usr/bin/br --port=/dev/ttyUSB0 e all_off');
header('Location: index.php'); //redirect user back to homepage after resetting
?>



seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: How to make php wait for an confirm box to finish

Postby seandisanti » Wed Apr 02, 2014 1:30 am

Yes, along the same lines as the previous suggestion, have your second file do the work in the backend, and pass execution to it via ajax, so you don't even have to reload the page, and you can output results etc on the users screen. It's a better look and does exactly what you want. Personally I like using jquery for ajax, as demonstrated here: http://www.w3schools.com/jquery/jquery_ ... t_post.asp. It only takes a couple of minutes to get it going and make sense of it the first time, then you'll want to use it for everything.

chenyeyu
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Apr 09, 2014 5:21 pm

Re: How to make php wait for an confirm box to finish

Postby chenyeyu » Wed Apr 09, 2014 5:30 pm

good


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest