18

How can I make a PHP input thing like prompt('example prompt') in javascript?

Not like a form, like a prompt().

3
  • Impossible. PHP is a server side language and can NOT pop up a window on a client machine. You need to use client-side Javascript. Commented Mar 9, 2014 at 2:23
  • 2
    Use PHP CLI. You won't get a fancy popup, but it can get user input from the command line. If you're talking about from apache/web, then no, there's no such thing. Commented Mar 9, 2014 at 2:24
  • Same question, actually answered for PHP CLI Commented Nov 11, 2016 at 0:21

3 Answers 3

22

Solution #1: Prompt for get input inside of anywhere code:

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

Sample output:

# 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 get input in firstly inline when run php:

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

Sample output:

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

Extra tips:

If you want to integrate CLI and WEB inputs, you can use this magic code:

<?php
if (PHP_SAPI === 'cli') parse_str(implode('&', array_slice($argv, 1)), $_GET);
var_dump($_GET);

And now write for use under CLI:

# php sample.php user=nabikaz website=nabi.ir

And follow the normal routine for the WEB:

http://localhost/sample.php?user=nabikaz&website=nabi.ir

Without defining the number and name of inputs.
Both have the same result. ;-)

array(2) {
  ["user"]=>
  string(7) "nabikaz"
  ["website"]=>
  string(7) "nabi.ir"
}
Sign up to request clarification or add additional context in comments.

Comments

11

You can't take input in the middle of php execution since it finishes before the page is actually shown to the user. However, you can get input using HTML and receive that using php. Here's a really basic example:

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

It takes the user input and reloads the page. Then, it echoes what the input was.

7 Comments

Ok sorry I thought you could do that. Does anybody know how to take data from JavaScript variables into PHP variables?
You're encountering basically the same problem. You would need to refresh the page and send the JavaScript variable in a similar method as in my answer.
Here's a question on SO with multiple solutions as answers: stackoverflow.com/questions/133925/…
@emileb I am the worst PHP coder, and I do not get that post.
The first answer gives you a javascript method which takes in param the url to the Php script that will need your data (in $_POST var), an associative array of the data you want to send to php (and optional method GET or POST). That method will do the rest for you, you only have to process the data that has been posted in your php script.
|
2

in php you better use form for inputs, however if you want you can use java script input and alert to read and display as follow.

echo "<script>input=prompt('Enter your name !'); alert(input);</script>";

1 Comment

You did not change anything and yet add a minus ??? this is my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.