为什么我的函数将布尔值更改为'on'? [英] Why is my function changing boolean value to 'on'?

查看:134
本文介绍了为什么我的函数将布尔值更改为'on'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对PHP /编程世界很陌生,我正在网上学习和其他形式,但我找不到任何东西来帮助回答我的问题,这就是为什么我在这里。任何帮助肯定是赞赏,谢谢!



我想将下面的代码转换为我可以调用的函数。它的工作原理与下面一样,如果我检查我的表单中的复选框,则输出1,如果我不触摸我的复选框,它将保持为0。

  $ activeMain =(isset($ _ POST ['activateBox']))? $ _POST ['activateBox']:false; 

if($ activeMain == true){
$ activeMain ='1';
}

然而,当我尝试使用函数来做同样的事情时,我选择我的复选框显示'1',它仍然是0,如果我做了一个var_dump输出现在是开,而不是1,如它应该是。



<下面是我试过的功能:

 函数activeCheck($ activeMain){

$ activeMain =(isset($ _ POST ['activateBox']))? $ _POST ['activateBox']:false;

if($ activeMain == true){
$ activeMain ='1';
}

return $ activeMain; //我使用返回值
搞砸了,据我所知,它没有任何作用。

} //结束activeCheck函数

activeCheck($ activeMain); //调用函数

总而言之,当我尝试使用函数以及如何使其工作时,为什么它会显示on。

编辑:

如何将我的原始代码(上面的代码的第一位) ?
我应该使用哪些值/我可以使用除_SESSION之外的其他值来检查用户是否已从表单中选中复选框?



我有一个HTML / PHP表单,我可以选择一个复选框。如果用户点击复选框,他们提供的输入将输出一个真值1。



我的HTML / PHP表单:

 <?php session_start (); ?> 
<!DOCTYPE HTML>
< HTML>
< head>
< title> PHP FORM< / title>
< / head>

< body>
< form method =postaction =processForm.php>

名称:< input type =textname =namesrequired =required>< br>

< input type =submitvalue =创建用户onclick =formNAMES>< br>

激活:< input type =checkboxname =activateBox>
<?php

if(isset($ _ SESSION ['error'])){
foreach($ _SESSION ['error'] as $ value){
echo $ value;
}
session_destroy();
unset($ _ SESSION ['error']);
}


/ *上面if语句检查是否在processForm页面中设置了$ _SESSION变量。如果有,
对应于错误的错误信息显示在重定向到这个表单上。未设置确保
$ _SESSION在完成该过程后被销毁。 * /

?>
< / form>
< / body>
< / html>


解决方案

将相关行更改为以下内容:

  $ activeMain = isset($ _ POST ['activateBox']); 

更新:请检查这一个。

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

activeCheck();

由于更新问题而更新的答案
我删除了 session_destory 如果你执行该操作,并且你有另一个会话变量ex: user isLoggedIn ,它也会被销毁。为此, unset 可以。请根据最佳实践检查XSS,围绕互联网实施逻辑的Sql注入攻击,以及在将进程参数存入数据库之前对您的数据进行验证/清理等。


 <?php 
// formView.php
ob_start();
session_start();
?>
<!DOCTYPE HTML>
< HTML>
< head>
< title> PHP FORM< / title>
< / head>

< body>
< form method =POSTaction =processForm.php>
名称:< input type =textname =namesrequired =required>< br>
< input type =submitvalue =创建用户onclick =formNAMES>< br>
激活:< input type =checkboxname =activateBox>
<?php

if(isset($ _ SESSION ['error'])){
foreach($ _SESSION ['error'] as $ value){
echo $ value;
}
unset($ _ SESSION ['error']);
}


/ *上面if语句检查是否在processForm页面中设置了$ _SESSION变量。如果有,
对应于错误的错误信息显示在重定向到这个表单上。未设置确保
$ _SESSION在完成该过程后被销毁。 * /

?>
< / form>
< / body>
< / html>
<?php ob_end_clean(); ?>


$ php
// processForm.php

if(!empty($ _ POST)){
$ safeParameters = [];
foreach($ _POST as $ key => $ val){
//清理输入。 @请参阅XSS,SQL注入等。
//根据您的需要验证参数。
$ safeParameters [$ key] = $ val;
}
$ _POST = [];
checkIsActivated($ safeParameters);
//实现其他逻辑,
//将表单保存到数据库等
}

函数checkIsActivated($ parameters)
{
return!empty($ parameters ['activateBox']);
}
?>


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 help is certainly appreciated, Thanks!

I want to turn the below code into a function that I can call. It works just as it stands below as in it outputs a 1 if I check my check box in my form and remains 0 if I don't touch my checkbox.

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

if ($activeMain == true) {
    $activeMain = '1';
} 

However when I try to use a function to do the same thing, and I select my checkbox to display a '1', it remains 0 and if I do a var_dump the output is now "on" instead of 1 like how it is supposed to be.

Below is the function I tried:

function activeCheck($activeMain) {

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

    if ($activeMain == true) {
        $activeMain = '1';
    } 

    return $activeMain;//I messed around with a return value
                           and as far as I can tell, it has no effect.

}//ends activeCheck function

activeCheck($activeMain);//call to function

In all I'm confused on why it shows "on" when I try to use a function as well as how to get it to work.

EDIT:

How do I turn my original code (first bit of code posted above) into a function? What values should I use / can I use something else besides _SESSION to check if user has selected the checkbox from the form?

I have a HTML/PHP form in which I give the option to select a checkbox. If users hit the checkbox, the input they provided will output a '1' for a true value.

My HTML/PHP form:

<?php 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;
        }
        session_destroy();
        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>

解决方案

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']);
}
?>

这篇关于为什么我的函数将布尔值更改为'on'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆