How to make a choice in php (no html please)

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Wed Mar 29, 2017 4:49 pm

Forgive the kindergarten question but can someone set me on the path to a basic choice for php.. Do not want to jump into html to do it..
And it needs to be after a few php commands have been inititated.. for example

Code: Select all

<?php
echo "Now is the time for all good men to go to work.<br>";
// Stop here with something like this.
echo "Enter 1 to Continue or 2 to exit<br>"
// not sure of the code input or what to make it wait for a decision..
if ($answer = 2) exit;
// rest of php code follows... if answer was 1...
?>
// I know its basic.. but if you don't know then you don't know.. Thanks..
// Prefer not to activate any html to help out.. i know how to do that.

chorn
php-forum Fan User
php-forum Fan User
Posts: 563
Joined: Fri Apr 01, 2016 2:18 am

Wed Mar 29, 2017 10:41 pm

where would this input come from? makes no real sense for a language that is designed for request-response

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 315
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Thu Mar 30, 2017 2:07 pm

You do realize that all PHP was meant for was to make HTML dynamic and not static. That is why people should learn HTML/CSS thoroughly before tackling PHP.

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Thu Mar 30, 2017 11:39 pm

Why would you want something like this? If you could explain what your trying to achieve it may be easier than trying to help with some strange solution.

If you want something that people can use on a command line, then have a look at the 'PHP CLI' as this allows you to run something interactively, but still doesn't achieve what your trying in the example above.

tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Fri Mar 31, 2017 11:40 am

Here is more infomation that may give better clues... pretty sure i have used that simple command but maybe not.. I am well acquainted with html and know exactly how to make it stop the php and ask for input the go back to php....

all i want to do is have the running program ask for input .. the take the input and make a decision.. do it all the time in basic but not sure if php has a similar command...

The sample above gives similar description but is repeated here with attempt to shed more light onto it....

php
php
php
php

$x =input "Contine Y or N"; (just don't think this needs to be html)

if ($x=="N") exit;
morephp
morephp
morepyp
Last edited by tommytx123 on Fri Mar 31, 2017 1:25 pm, edited 1 time in total.

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Fri Mar 31, 2017 12:08 pm

Sorry for asking what your trying to do! Sometimes there may be alternatives to what your trying to achieve which may help or be of interest.

But as you don't seem to care about what other people think or even able to do a simple search for what your after then have a look at http://www.php.net/manual/en/features.c ... .php#94924

tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Fri Mar 31, 2017 12:50 pm

Sorry Nigel but did not realize it sounded like i was yelling at you.. i have reworded the comment.. but I have used that same idea before so i know it can be done with php and no html but all my searching for weeks has not turned up anything that works..

Your example is a perfect one of exactly what i am trying to do but i came across that one weeks ago and as good as it looks it still did not work for me..
So thanks for that .. I will post it here in the hopes that someone may see what is wrong with it... must be something simple..


Here is the output.. just goes thru w/o waiting for any key.
Are you sure you want to do this? Type 'yes' to continue: ABORTING!
Straight thru w/o waiting for a response.. could me something i need to add but don't see anything..

Posted here in case someone can see what is wrong..

Code: Select all

If you want to be interactive with the user and accept user input, all you need to do is read from stdin. 

<?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";
?>

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Fri Mar 31, 2017 11:56 pm

Hmmmm... I've tried it on Linux(ubuntu php 7.0.15) and Windows (10 php 7.1.3) and it works on both of them. Which version of php are you running and what platform?

tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Sun Apr 02, 2017 12:40 am

Tried on one Linux 5.6.20 and one on LInux 5.6.30 and this one example has been around a long time so no reason to think it would need a later version of php than I have.. i don't have php on my windows 10..but need to use it on the web.. so that wont help..

Went back and looked at that post and it says written 7 years ago..

I just tried another hundred or so examples that claim to work but will not wiat for the input to be entered..

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 754
Joined: Mon Feb 22, 2016 5:52 pm

Sun Apr 02, 2017 5:36 am

Copy the following code and paste it into a a new page in your text editor:

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Simple php input and decision trial</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php
# Declare form to be displayed as a variable as it's used in two places
$form = "Enter 1 to Continue or 2 to exit<br>
  <form method='post' action='input.php'>
    <fieldset><legend>Input</legend>
      <label for='input'><input type='text' name='input' value='' /></label>
      <button type='submit' name='submit' value='Submit'>Submit</button>
    </fieldset>
  </form>
</body>
</html>";

if (isset($_POST['submit'])) {
  /*
     If the user has submited the form (as in they have entered some input), execute this part of the program
  */
  $input = filter_input(INPUT_POST, 'input', FILTER_VALIDATE_INT);
  if ($input == 2) {
    echo "Thank you for coming, goodbye!";
    exit();# Blank page
  } else {
    if ($input == 1) {
      echo "I'm glad that you decided to stay a while<br>";
    }
    echo $form;
  }
} else { # this will display the form if it is the first time loading up
  echo $form;
}
?>
Then save the file as "input.php" into your server folder (on your local machine), this is the folder where your web pages are kept and is known as root, if you are not sure where that is, read the documentation when you installed the server.

Using your browser, type "localhost/input.php" into the web address bar and you should have an input field displayed which you can use to provide input. See if this is what you mean.

If not, then you will have to explain clearly what it is you are looking to achieve - what you are using (computer, operating system etc..) and how you are using it and how you expect to know that you have achieved what you want.

PHP is not like other languages which wait for something to happen where it loops until until input is provided, PHP wants to execute the script and close, if it does not execute within a time then it closes anyway. It is designed to be used in response to a request from a browser.

Although (as mentioned above) PHP can be used from the command line (terminal, dos window, etc..) it was designed for and is best used from a browser.

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Sun Apr 02, 2017 5:51 am

Strange - have you tried...

Code: Select all

<?php
while (FALSE !== ($line = fgets(STDIN))) {
   echo $line;
}
?>
Can you post a screen capture of what you get? (Or some redirect the output to a file)

tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Mon Apr 03, 2017 6:11 am

Screen capture w/ cnt: https://www.screencast.com/t/MoYCrkuCa

Code: Select all

<?php
$cnt = 0;
while (FALSE !== ($line = fgets(STDIN))) {
   echo $line;
}
?>

Screen capture with cnt added: https://www.screencast.com/t/MoYCrkuCa

Code: Select all

<?php
$cnt = 0;
while (FALSE !== ($line = fgets(STDIN))) {
   echo $line;
$cnt++;
echo "$cnt ";
}
?>
Added the cnt just to see if any looping nothing...
I am pretty sure i have done this in the past and all the google searches say that it works but not on my Apache.
i also tried it on hostgator and on my dedicated server same results.
Screen shot blank in all cases...
Am i losing my mind or did you ask me to run this test before.. I am using a file name called new.php to keep dumping code for testing and when i went to the test file that exact code yu gave me above was in the new.php test file. but i have searched until i am blue loooking for ideas that i may have come across your exact example and had tried it before... but i tried again with same negative results..

When i search google there are thousands of examples of making php wait till a decision is made.. and they all claim to work... but none work for me very odd.... and not like its my server as i tried it on a hostgator server also...

tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Mon Apr 03, 2017 6:27 am

Thanks for that input Hyper... I will try that later today and let you know..
Doubt that will work for me but will give it a try and see if i can get it to do it...
As i am very familiear with html forms and can do that one pretty easy.. but will see if any way i can get your idea to work..
I just want to stop a full blown php file midstream and then contine or stop.. not rocket science.. should be simple but
i do understand php does like to get the job done and walk away... but do have an idea that will work if all else fails..

1. grab the top half of the program and run it.. when it runs out and prints its results on the screen..
then i let the html form take over and wait for me to tell it continue or stop a nd simply send that result
to the server which will then either load the rest of the php or stop..
and i can run another copy of the full php w/o the stop html so that it would then run the full program end to end with no
stop... as far as i know it will be ok to run the first part twice.. should not matter...

2. What the first part does is gathere some data.. and based on the data it gatehrs i will run or go back and put some
more gas in the truck.. for exampe to simplify.. the first part reads the gas gage and says you have .01 gal of gas so i need to quit
and add more gas NOW.. else go on the trip..

3. So as last resort i can make a copy of the code that reads the gas tank... and based on that will run the full program.. but i do remeember doing this before so should not be so hard...

But thanks for your input.. it may help in the long run.. if all else fails.

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Mon Apr 03, 2017 1:05 pm

I get something like ( file saved as test.php )...

Code: Select all

nigel@Laravel:~$ php test.php 
adakshd
adakshd
ajdakjd
ajdakjd
^C
nigel@Laravel:~$ 

tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Wed Apr 05, 2017 7:38 pm

Here you go Hyper can't get it to work for me....can you run what i did to see if it will work for you.. as php just does not like to wait around for a decision..

file name = input.php
&&&&&&&&&&&&&&&

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Simple php input and decision trial</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php
# Declare form to be displayed as a variable as it's used in two places
$form = "Enter 1 to Continue or 2 to exit<br>
  <form method='post' action='input.php'>
    <fieldset><legend>Input</legend>
      <label for='input'><input type='text' name='input' value='' /></label>
      <button type='submit' name='submit' value='Submit'>Submit</button>
    </fieldset>
  </form>
</body>
</html>";

if (isset($_POST['submit'])) {
  /*
     If the user has submited the form (as in they have entered some input), execute this part of the program
  */
  $input = filter_input(INPUT_POST, 'input', FILTER_VALIDATE_INT);
  if ($input == 2) {
    echo "Thank you for coming, goodbye!";
    exit();# Blank page
  } else {
    if ($input == 1) {
      echo "I'm glad that you decided to stay a while<br>";
    }
    echo $form;
  }
} else { # this will display the form if it is the first time loading up
  echo $form;
}
?>
Filename = index.php
&&&&&&&&&&&&&&

Code: Select all

<?php
Echo "Now is the time for all good men to come to the aid of their country.<br>";
include("input.php");
Echo "This is more php after the stop signal. Now is the time for all good men to come to the aid of their country.<br>";
?>
You can see its not exactly what you suggested but I am sure you can see what it has to do... stop the program in mid stream and the contineu
It simply walks right on past the stop and completes the entirre program in one flail swoop

So looks like the only thing i can do is something like this..

Code: Select all

<?php
include("firsthalfofpgm.php");
?>
<form action="secondhalfofpgm.php">
    <input type="submit" value="Continue" />
</form>
But thanks for all the help guys... not what i wanted its easy to do with the simple html form.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 754
Joined: Mon Feb 22, 2016 5:52 pm

Thu Apr 06, 2017 9:44 am

Something isn't right, you have been given 2 solutions which both work.

Can you create the following file and run in a browser, and let us know the following:
Version of PHP (should be at the top)
System (should be next line down)
Server API (should be the next line)

As in the top three lines, if you copy and paste these as I have done below:
PHP Version 7.0.15-0ubuntu0.16.10.4
System Linux hyper-TEXT-327-Z-14g 4.8.0-41-generic #44-Ubuntu SMP Fri Mar 3 15:27:17 UTC 2017 x86_64
Server API Apache 2.0 Handler

Post Reply