15

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

Not like a form, like a prompt().

user987339
  • 9,680
  • 8
  • 38
  • 42
user3397425
  • 151
  • 1
  • 1
  • 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. – Marc B Mar 09 '14 at 02:23
  • 2
    Use [PHP CLI](http://www.php.net/manual/en/features.commandline.php). 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. – Dave Chen Mar 09 '14 at 02:24
  • [Same question](/q/5794030/1157100), actually answered for PHP CLI – 200_success Nov 11 '16 at 00:21

3 Answers3

13

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!
Nabi K.A.Z.
  • 6,655
  • 3
  • 42
  • 58
10

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.

Anonymous
  • 10,924
  • 6
  • 31
  • 56
  • 1
    Ok sorry I thought you could do that. Does anybody know how to take data from JavaScript variables into PHP variables? – user3397425 Mar 09 '14 at 02:37
  • 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. – Anonymous Mar 09 '14 at 02:38
  • 1
    Here's a question on SO with multiple solutions as answers: http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit – Emile Bergeron Mar 09 '14 at 02:39
  • @emileb I am the worst PHP coder, and I do not get that post. – user3397425 Mar 09 '14 at 02:57
  • The first [answer](http://stackoverflow.com/a/133997/1218980) 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. – Emile Bergeron Mar 09 '14 at 03:35
  • Thanks I have just figured out what Anonymous' answer meant. Thanks! – user3397425 Mar 09 '14 at 08:10
  • As said in question: "Not like a form", so this is the best answer: https://stackoverflow.com/a/47763306/1407491 – Nabi K.A.Z. Dec 11 '17 at 23:52
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>";
Nikolai Shevchenko
  • 4,616
  • 8
  • 28
  • 32
Msfata
  • 21
  • 2