Assigning the result of a JavaScript confirm box t

2019-03-01 03:41发布

//this is in php
function msgbox($msg, $type)
    {
    if ($type == "alert")
        {
        // Simple alert window
        ?> <script language="JavaScript"> alert("<? echo $msg; ?>"); </script> <?
        }
    elseif ($type == "confirm")
        {
        // Enter Confirm Code Here and assign the $result variable for use
        // Should include "OK" and "Cancel" buttons.
        ?>
           <script language="JavaScript">
           if (confirm("<? echo $msg; ?>"))
                {
                <? $result == "ok"; ?>
                }
           else
                {
                <? $result == "cancel"; ?>
                }
           </script>
        <?
        }
    }



if ($page_title->exists())

{msgbox("page exists,do you want to delete", "confirm");

}      
 if ($result == "ok")
//code..

The problem is that $result is not reading the value from the confirm box I think because the if clause is not being executed and the program flow is going where it would go without the if clause.

3条回答
Emotional °昔
2楼-- · 2019-03-01 04:00

What you are trying to accomplish can be created with Ajax, as long as the page is created on server side and then send to user, you cannot directly tamper $result variable as you intended. First grasp a reference of AJAX and how to use it..

101 article on Ajax with jQuery (sitepoint.com)

Simple implementation of AJAX with jQuery and PHP

Client.html

<!--some html...-->
<a class="ajax" href="/delete.php?title=some+title">Delete action link</a>

<script type="text/javascript">
// assuming jQuery has been loaded
$(function () {
    $('a.ajax').click(function () {
        // get link's href, get main url part and query part
        var link = $(this).attr('href');
        var route = link.substring(0, link.lastIndexOf('?'));
        var query = link.substring(link.lastIndexOf('?') + 1);

        // perform ajax call, to the main part of the link, with data
        $.ajax({
            type: "GET",
            url : route,
            data : query,
            success : function (data) {
                if (data === '1') {
                    window.alert('page removed');
                } else {
                    window.alert('error');
                }
            }
        });

        // prevent default behavior
        return false;
    });
});    
</script>

And a delete.php script, which takes $_GET['title'] as parameter

<?php
    $title = $_GET['title'];
    if ($pages->contain($title)) {
        $pages->remove($title);
        echo '1';
    }
?>

Note, that this is just simplified, to show you, how a simple AJAX call can be done

查看更多
唯我独甜
3楼-- · 2019-03-01 04:03

You are using short tags, make sure that they are turned on from php.ini file otherwise php code won't execute in your code.

查看更多
地球回转人心会变
4楼-- · 2019-03-01 04:11

You cannot mix server-side code (PHP) with client-side code that way. For javascript to change PHP-state, you need to make a HTTP-call (AJAX is often used).

You need to read a PHP-tutorial and make sure you grasp the concepts.

查看更多
登录 后发表回答