多个变量的if语句

时间:2014-12-22 10:22:41

标签: php html forms if-statement statements

已找到解决方案!谢谢!


这是解决方案,我必须为每个$_POST变量创建一个IF语句。


原始问题

所以我有一个HTML表单,在提交时,我的PHP文件将数据添加到一个新文件,我的问题是我的if语句。这是我目前的代码;

HTML(表格)

<form id="msform" action="result.php" method="post">
    <!-- progressbar -->
    <ul id="progressbar">
        <li class="active">Basic Details</li>
        <li>Server Options</li>
        <li>Final Touches</li>
    </ul>
    <!-- fieldsets -->
    <fieldset>
        <h2 class="fs-title">Add some basic details...</h2>
        <h3 class="fs-subtitle">This is step 1</h3>
        <input type="text" name="levelName" placeholder="Level Name" />
        <input type="text" name="messageOFTD" placeholder="Message of The Day" />
        <input type="text" name="port" placeholder="Server Port (Default is 25565)" />
        <input type="button" name="next" class="next action-button" value="Next" />
    </fieldset>
    <fieldset>
        <h2 class="fs-title">Customize your server...</h2>
        <label for="players">Max No. of Players</label>
        <input type="text" name="maxPlayers" placeholder="Maximum No. of Players" />
        <label for="select">Default Gamemode</label>
        <select value="select" name="defaultGamemode">
            <option value="SURVIVAL" name="survival">Survival</option>
            <option value="CREATIVE" name="creative">Creative</option>
            <option value="ADVENTURE" name="adventure">Adventure</option>
            <option value="SPECTATOR" name="spectator">Spectator</option>
        </select>
        <p>Force Gamemode</p>
        <input type="checkbox" name="gplus" />
        <p>Difficulty</p>
        <select value="select">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
        <p>Allow Flight</p>
        <input type="checkbox" name="allowFlight" />
        <p>Enable PvP</p>
        <input type="checkbox" name="gplus" />
        <p>Enable Command Blocks</p>
        <input type="checkbox" name="gplus"  />
        <p>Spawn Animals</p>
        <input type="checkbox" name="gplus"  />
        <p>Spawn NPC's</p>
        <input type="checkbox" name="gplus" />
        <p>Spawn Monsters</p>
        <input type="checkbox" name="gplus" />

        <p>Hardcore Mode</p>
        <input type="checkbox" name="gplus" />

        <p>Allow Nether</p>
        <input type="checkbox" name="gplus" />

        <p>Generate Structures</p>
        <input type="checkbox" name="gplus" />
        <p>Announce Achievements</p>
        <input type="checkbox" name="gplus" />


        <input type="button" name="previous" class="previous action-button" value="Previous" />
        <input type="button" name="next" class="next action-button" value="Next" />
    </fieldset>
    <fieldset>
        <h2 class="fs-title">A few more settings</h2>
        <h3 class="fs-subtitle">You're almost done!</h3>

        <p>Online Mode</p>
        <input type="checkbox" name="gplus" />
                <p>Enable Query</p>
        <input type="checkbox" name="gplus" />

        <p>Enable Snooper</p>
        <input type="checkbox" name="gplus" />

                <p>Enable RCON</p>
        <input type="checkbox" name="gplus" />

        <p>View Distance</p>
        <input type="text" name="viewDistance" placeholder="Default is 10" />
        <p>Level Seed</p>
        <input type="text" name="levelSeed" />
        <p>Resource Pack</p>
        <input type="text" name="pack" placeholder="Place URL of Pack Here" />

        <input type="button" name="previous" class="previous action-button" value="Previous" />
        <input type="submit" value="Submit" />
    </fieldset>
</form>

PHP

if (isset($_POST["levelName"])){
$txt = $levelname . $_POST["levelName"] . "\n";
fwrite($myfile, $txt);
} else {
$txt = "level-name=NO_NAME_GIVEN";
fwrite($myfile, $txt);
}

正如您所看到的,我想检查来自html表单的输入表单是否为空白,如果是,我想为其分配一个字符串并将该字符串添加到文件中,如果它包含数据,它将添加该数据。它可以很好地添加数据,但如果没有数据,它就不会显示任何数据。那么这里的问题是什么?也!如何为不同的输入表单创建多个这些IF语句?

感谢任何和所有的帮助:)如果这个问题看起来模糊不清,我会尽情说明,如果需要,我会添加所需的任何必要信息:)再次感谢!

4 个答案:

答案 0 :(得分:1)

对于多个if语句使用else-if语句,这将允许您具有多个条件和1个默认消息。例如:

if (isset($_POST["levelName"]))
{
  $txt = $levelname . $_POST["levelName"] . "\n";
  fwrite($myfile, $txt);
} 
else if (isset($_POST["levelName2"]))
{
  $txt2 = $levelname2 . $_POST["levelName2"] . "\n";
  fwrite($myfile, $txt2);
} 
else 
{
  $txt = "level-name=NO_NAME_GIVEN";
fwrite($myfile, $txt);
}

要检查某些内容是否未输入或是否为空白,您可以使用:

if(!isset($_POST["levelName"]) || isset($_POST["levelName"]) == '')
{
   echo "You did not fill out the required fields.";
}

编辑:但是根据你评论的内容,你需要一个单独的if-else语句(就像你在原帖中所说的那样)1,你需要检查每个不同的字段,哪个字段会测试该字段的不同条件。

答案 1 :(得分:1)

试试这个

if (isset($_POST["levelName"])){
if($_POST["levelName"] != ""){
$txt = $levelname . $_POST["levelName"] . "\n";
fwrite($myfile, $txt);
} else {
$txt = "level-name=NO_NAME_GIVEN";
fwrite($myfile, $txt);
}
}//submit close

答案 2 :(得分:0)

如果我是你,

我将按照以下方式执行此操作

            <?php
                $heading_text = array(
                    'levelName' => 'Lelvel-name',
                    /**
                    Your other text for various fileds,just make sure that the 
                    array keymatch with your form elements name
                    ...
                    */
                );

                $_POST = array_filter( function($v){
                    if( !$v || ctype_space( $v))
                        return false;

                    return true;
                }, $_POST);

                $default_values = array(
                    'levelName' => 'NO_NAME_GIVEN'
                    /**
                    set the value if not a vali value is provided 
                    array keymatch with your form elements name
                    ...
                    */
                );

                $filtered_arr = array_merge( $default_values, $_POST);

                foreach($filtered_arr as $k => $v){
                    $text = $heading_text[$k].'--'.$v;
                    fwrite( $myfile, $text);
                }
            ?>

答案 3 :(得分:-1)

你需要检查非空虚而不是存在。因此,请尝试将isset更改为!empty

if (!empty($_POST["levelName"])){