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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号