How to take user input like prompt (), but in PHP?

How can I do PHP text input as prompt('example prompt') in javascript?

Not like a form like prompt() .

+6
source share
2 answers

You cannot accept input in the middle of php execution, as it ends before the page is shown to the user. However, you can get input using HTML and get it using php. Here is a really basic example:

 <?php echo $_POST['value']; ?> <form method="post" action=""> <input type="text" name="value"> <input type="submit"> </form> 

It takes user input and reloads the page. Then he repeats what was at the entrance.

+6
source

Solution # 1: request input inside any code:

 <?php echo "What do you want to input? "; $input = rtrim(fgets(STDIN)); echo "I got it:\n" . $input; 

Output Example:

 # php test.php What do you want to input? Hello, I'm here! I got it: Hello, I'm here! 

Solution # 2: If you want to get input in the built-in line when starting php:

 <?php $input = $argv[1]; echo "I got it:\n" . $input; 

Output Example:

 # php test.php "Hello, I'm here!" I got it: Hello, I'm here! 
+1
source

All Articles