douchuo0730 2014-01-26 14:47
浏览 33
已采纳

我怎么能在PHP中对话回答是或否,根据答案做php代码? [重复]

This question already has an answer here:

I figured how i can get a dialog and seems like i need to use javascript but this doesnt let me follow up with php code depending on the answer unless there is a way?

<?php


$ime = "Marko";

echo <<< EOT
<SCRIPT language="JavaScript">

var hi= confirm("Do you really want to deactivate $ime?");
if (hi== true){
    <?php ?>
    alert("$ime Deactivated!");
    // I need to do php code here...
}else{
    alert("Skipping $ime");
    // I need to do php code here...
}


</SCRIPT>


EOT;

?>
</div>
  • 写回答

5条回答 默认 最新

  • doujiang1939 2014-01-26 15:12
    关注

    Improving on @papirtiger's answer, you could use AJAX to pass onto the PHP code seamlessly without a page reload.

    All AJAX really is is the way you can pass data from javascript to a PHP file. Because PHP is server side, any PHP script that needs to be executed MUST be on the server. PHP generates the script and sends it to the client and the connection between the client and the server is done when the generated page downloads. Javascript can continue its work as it is client side. But since it's on the client's machine, it can't run a PHP script inside it as it can't be interpreted. AJAX takes data from PHP and sends it like a Form would (using POST or GET variables), executes a separate PHP file on the server, grabs the response from the script and puts it into the javascript without the entire page reloading.

    Here is what your script could look like.

    <script>
        if (window.confirm("Are you sure?")){
            // Begin the AJAX function
            $.ajax({
                // Declare the type (GET or POST)
                type = "POST",
                // The local server location to the PHP script to get the response from
                url = "yes.php",
                // Data to send (in this case nothing needs to be sent)
                data = "",
                // Get the response if a script is executed successfully
                success: function(response) {
                    // Display the response from the script in an alert box
                    alert(response);
                }
            )};
        } else {
            // Rinse and repeat using another file
            $.ajax({
                type = "POST",
                url = "no.php",
                data = "",
                success: function(response) {
                    alert(response);
                }
            )};
        }
    </script>
    

    You would also need to include the jQuery library in the head of your HTML otherwise this AJAX markdown will not work and you would have to result to traditional (and ugly/messy) pure javascript AJAX executions.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    

    Hope this helps! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效