Why is my function changing boolean value to 'on'?

前端 未结 1 1365
轮回少年
轮回少年 2021-01-29 06:07

So I\'m new to php/the programming world and I\'m studying online and other forms and such but I couldn\'t find anything to help answer my question which is why I\'m here. Any h

相关标签:
1条回答
  • 2021-01-29 06:45

    Change related line to following: The problem is when activateBox is not empty it assings itself to $activeMain naturally.

    $activeMain = isset($_POST['activateBox']);
    

    Updated : Check this one.

    function activeCheck() {
        return isset($_POST['activateBox']);
    }
    
    activeCheck();
    

    Updated Answer Due To Updated Question : I removed session_destory if you execute that and if you have another session variable ex: user isLoggedIn it would be destroyed too. unset is OK for the purpose. Please check XSS, Sql injection attacks around the internet implement logic according to best practises, and validate/sanitize your data before process parameters into DB or etc.

    <?php
    // formView.php
    ob_start();
    session_start();
    ?>
    <!DOCTYPE HTML>
    <HTML>
        <head>
            <title>PHP FORM</title>
        </head>
    
    <body>
        <form method="POST" action="processForm.php">
        Name: <input type="text" name="names" required = "required"><br>
        <input type="submit" value="Create Users" onclick="formNAMES"><br>
        Activate: <input type="checkbox" name="activateBox">
        <?php
    
        if (isset($_SESSION ['error'])) {
                foreach ($_SESSION['error'] as $value) {
                    echo $value;
                }
                unset($_SESSION['error']);
        }
    
    
        /* Above if statement checks if $_SESSION variable has been set in processForm page. If it has, 
        an error message corresponding to the error shows up on redirect to this form. The unset makes sure 
        the $_SESSION is destroyed upon completion of the process. */
    
        ?>
        </form>
    </body>
    </html>
    <?php ob_end_clean(); ?>
    
    
    <?php
    // processForm.php
    
    if (!empty($_POST)) {
        $safeParameters = [];
        foreach ($_POST as $key => $val) {
            // sanitize your inputs. @see XSS, SQL injection etc.
            // validate parameters according to your needs.
            $safeParameters[$key] = $val;
        }
        $_POST = [];
        checkIsActivated($safeParameters);
        // implement other logic,
        // save form to database etc.
    }
    
    function checkIsActivated($parameters)
    {
        return !empty($parameters['activateBox']);
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题