Equivalent of Alert() and Prompt() in PHP

戏子无情 提交于 2019-12-05 19:01:41

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);

?>

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.

That's it:

$shouldProceed = readline('Do you wanna proceed?(y/n): ');
if (strtolower(trim($shouldProceed)) == 'n') exit;
proceed();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!