(isset $ _POST ['submit'])条件始终为false

时间:2015-02-22 07:10:11

标签: php html post

我在名为addSection.php

的页面中编写了以下代码

<from class="mainSettingsForm add" action="" method="post">
    <h1>Add new section</h1>
    <p>
        <label>new section</label>
        <input type="text" name="section_name" placeholder="section title">
        <label>section status</label>
        <select name="sectionStatus">
            <option value="active">active</option>
            <option value="disActive">disActive</option>
        </select>
    </p>
    <p>
        <label>section location</label>
        <select name="sectionLocation">
            <option value="Side">Side</option>
            <option value="Body">Body</option>
        </select>
    </p>
    <label>section description</label>
    <textarea name="sectionDesc" placeholder="Section description"></textarea>
    <input class="btn-primary" type="submit" name="submit" value="Add">


</from>

以及名为Sections.php

的页面中的以下代码

<h2><a href="?page=sections&action=add">Add new section</a></h2>
<?php

if ($_POST OR @$_GET['action'])
{
    if (isset($_GET['action']) AND $_GET['action']=="add")
    {
        include 'views/addSection.php';
        if (isset($_POST['submit'])&&$_POST['submit']=="Add")
            echo 'ok';

    }

}
else
{
   include 'views/sections.php';
}

?>

这句话 if(isset($ _ POST ['submit'])&amp;&amp; $ _ POST ['submit'] ==“Add”)             echo'ok'; 永远不会被执行,因为它总是给出错误的值,如何使isset($ POST ['submit'])语句给出真值来执行条件?

2 个答案:

答案 0 :(得分:0)

您的陈述永远不会成立,因为第二个IF语句仅适用于$_GET变量,然后您尝试访问其中的$_POST。你不能同时进行GET和POST。

你也有一些错别字 - $_GET['action']将&#34;添加&#34;不是&#34;添加&#34;并且您的HTML中有<from>而不是<form>

if ( isset( $_POST['submit'] ) && $_POST['submit'] == "Add" ){
    echo "this is a POST and submit is add";
} elseif ( isset( $_GET['action']) && $_GET['action'] == "Add" ){
    echo "this is a GET and submit is add";
} else {
    include 'views/sections.php';
}

答案 1 :(得分:0)

请尝试我的代码。这将帮助您了解您传递的是什么。我还修复了表单标签 - 正如@Hobo Sapiens建议的那样。

<?php print_r($_REQUEST);?>
<form class="mainSettingsForm add" action="" method="post">
    <h1>Add new section</h1>
    <p>
        <label>new section</label>
        <input type="text" name="section_name" placeholder="section title">
        <label>section status</label>
        <select name="sectionStatus">
            <option value="active">active</option>
            <option value="disActive">disActive</option>
        </select>
    </p>
    <p>
        <label>section location</label>
        <select name="sectionLocation">
            <option value="Side">Side</option>
            <option value="Body">Body</option>
        </select>
    </p>
    <label>section description</label>
    <textarea name="sectionDesc" placeholder="Section description"></textarea>
    <input class="btn-primary" type="submit" name="submit" value="Add">


</form>