garyed Posted January 7, 2016 Share Posted January 7, 2016 I've been trying to pause a php script until a key is pressed by the user & I can't get it working. I've found quit a few answers online that seem to work for everyone else but not on my system. I'm running: PHP Version 5.5.9-1ubuntu4.5Below is a few of the codes I have tried but none of them accept any input & just run the script straight through to the end of the code. <?php echo "Are you sure you want to do this? Type 'yes' to continue: "; $handle = fopen ("php://stdin","r"); $line = fgets($handle); if(trim($line) != 'yes'){ echo "ABORTING!\n"; exit; } echo "\n"; echo "Thank you, continuing...\n"; ?> ------------------------------------------ <?php echo "Enter your name\n"; // Output - prompt user $name = fgets(STDIN); // Read the input echo "Hello $name"; // Output - Some text exit(0); // Script ran OK ?> ------------------------------------------------- <?php function anykey($s='Press any key to continue...') { echo "\n".$s."\n"; fgetc(STDIN); } anykey; echo "Help"; ?> ------------------------------------------------------- <?php define( 'STDIN', fopen( 'php://stdin', 'r' )); function prompt( $msg ) { echo $msg . "\n"; ob_flush(); $in = trim( fgets( STDIN )); return $in; } $key = prompt( 'Please press a key' ); echo "You pressed $key\n"; ?> Does anyone have any ideas what I'm missing? Quote Link to comment https://forums.phpfreaks.com/topic/300215-pause-until-keyboard-input/ Share on other sites More sharing options...
ginerjm Posted January 7, 2016 Share Posted January 7, 2016 Um..... You must be new at this PHP stuff. PHP runs on the server and doesn't have a "user interface". Consequently you cannot pause a script and await a keypress of any other user input. What you want is probably some kind of JS code that does what you want, but in that case it would be not so much of a "pause" but rather an "event handler" that gets triggered by a 'keypress' or 'onclick' event. Quote Link to comment https://forums.phpfreaks.com/topic/300215-pause-until-keyboard-input/#findComment-1529242 Share on other sites More sharing options...
garyed Posted January 7, 2016 Author Share Posted January 7, 2016 Um..... You must be new at this PHP stuff. PHP runs on the server and doesn't have a "user interface". Consequently you cannot pause a script and await a keypress of any other user input. What you want is probably some kind of JS code that does what you want, but in that case it would be not so much of a "pause" but rather an "event handler" that gets triggered by a 'keypress' or 'onclick' event. So you're saying it can't be done through php without using a client side scripting language. I didn't think it could but when I saw these different codes online it sure looked like people were doing it with php alone. Can you explain how any of those codes work or are they all wrong? Quote Link to comment https://forums.phpfreaks.com/topic/300215-pause-until-keyboard-input/#findComment-1529244 Share on other sites More sharing options...
Jacques1 Posted January 7, 2016 Share Posted January 7, 2016 PHP does have a command-line interface, and your script works just fine when executed from the console. What exactly happens on your PC? Are you saying you see all output but cannot input anything? Quote Link to comment https://forums.phpfreaks.com/topic/300215-pause-until-keyboard-input/#findComment-1529245 Share on other sites More sharing options...
garyed Posted January 7, 2016 Author Share Posted January 7, 2016 PHP does have a command-line interface, and your script works just fine when executed from the console. What exactly happens on your PC? Are you saying you see all output but cannot input anything? Thanks for the clarification, That's exactly what I didn't understand. I didn't know that I could run php through the terminal. When i tried it through the terminal things worked fine but I was trying to run those scripts as a web page through my Apache server so there were no pauses in the execution of the script. I nested an html button to do what i needed inside the php code but I wanted to experiment to see if i could do a pause with php until the user is ready. What i'm really doing is having a script to encode a session & I wanted a pause to give the user time to write the filename down they chose before returning them to another page. Quote Link to comment https://forums.phpfreaks.com/topic/300215-pause-until-keyboard-input/#findComment-1529250 Share on other sites More sharing options...
ginerjm Posted January 7, 2016 Share Posted January 7, 2016 You could simulate this 'conversation' by sending your beginning screen with a couple of buttons on it that will send the user's input back to the script. When the script is called it checks which button was clicked and decides what to do for you based upon that. Once your work is done your script can then send a new screen back to the client and await the next response. (I keep forgetting about the command-line PHP. Probably because I have never used it.) Quote Link to comment https://forums.phpfreaks.com/topic/300215-pause-until-keyboard-input/#findComment-1529251 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.