简体   繁体   中英

PHP version of confirm() of JavaScript

Is there a PHP version of JavaScript's confirm() function?
If not, what are my other options or how do I make something similar to the confirm()?

Because PHP is a server-side language (all the PHP code is executed on the server, and the output of the code is sent to the client), you'd have to make an HTML form with OK/Cancel buttons that would submit to your PHP page. Something like this:

confirm.php:

<p>Are you sure you want to do this?</p>

<form action="page2.php" method="post">
    <input type="submit" name="ok" value="OK" />
    <input type="submit" name="cancel" value="Cancel" />
</form>

page2.php:

<?php

if (isset($_POST['ok'])) {
    // They pressed OK
}

if (isset($_POST['cancel'])) {
    // They pressed Cancel
}

?>

Using the same answer from the previous post adding some error treatment and safety:

<form action="page2.php" method="post">
   Your choice: <input type="radio" name="choice" value="yes"> Yes <input type="radio" name="choice" value="no" /> No
    <button type="submit">Send</button>
</form>

And in your page2.php:

if (isset($_POST['choice']) /* Always check buddy */) {
    switch($_POST['choice']) {
        case 'yes':
            /// Code here
            break;
        case 'no':
            /// Code here
            break;
        default:
            /// Error treatment
            break;
    }
}
else {
    // error treatment
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM