Equivalent of Alert() and Prompt() in PHP

本小妞迷上赌 提交于 2019-12-10 09:37:20

问题


In JavaScript, we have Alert() and Prompt() which open up a popup box for the user.

Is there an equivalent for PHP? $Get_['asdf'] is one way to get user input... any others?

Also, one more question. Is it a requirement that PHP always be executed all at once? Or can it be like JavaScript, where it waits for the user input (e.g. popup box), then executes the rest of the code after that.


回答1:


PHP is a server side language, it can't do alert messages on the client side. But you can use javascript within the php to do the alert.

<script type="text/javascript">
window.alert("Hi There, I am the Alert Box!")
</script>

For Prompt you can do something like this -

<?php

    //prompt function
    function prompt($prompt_msg){
        echo("<script type='text/javascript'> var answer = prompt('".$prompt_msg."'); </script>");

        $answer = "<script type='text/javascript'> document.write(answer); </script>";
        return($answer);
    }

    //program
    $prompt_msg = "Please type your name.";
    $name = prompt($prompt_msg);

    $output_msg = "Hello there ".$name."!";
    echo($output_msg);

?>



回答2:


Nope, there is no equivalent. All php is executed on the server-side only. Unless you're using it at the command-line, which I doubt.

It also cannot wait for user input like javascript, like you wanted. Sorry. You'll have to use ajax for that.




回答3:


That's it:

$shouldProceed = readline('Do you wanna proceed?(y/n): ');
if (strtolower(trim($shouldProceed)) == 'n') exit;
proceed();


来源:https://stackoverflow.com/questions/30388997/equivalent-of-alert-and-prompt-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!