MyNameIsNotSoCool
MyNameIsNotSoCool

Reputation: 133

Multiple if statements for multiple variables

A Solution has been found! Thank you!


Here's the solution, I had to create an IF statement for each of the $_POST variables.


Original Question

So I have an HTML Form which when submitted, my PHP file adds the data to a new file, what my problem is are my if statements. Here's my current code;

HTML (Form)

<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);
}

As you can see, I want to check if an input form from the html form is blank, if it is, I want to assign a string to it and add that string to the file, if it contains data, it will add that data. It adds the data fine, but if there's no data, it doesn't show anything. So what's the problem here? Also! How would I create multiple of these IF statements for different input forms?

Thanks for any and all help :) I apoligze if this question seems vague, I will add any necessary information required if requested :) Thanks again!

Upvotes: 0

Views: 756

Answers (4)

Abhisek Malakar
Abhisek Malakar

Reputation: 899

If I were you,

I will do this as follows

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

Upvotes: 0

NoLiver92
NoLiver92

Reputation: 892

For multiple if statements use else-if statements, this will allow you to have multiple criteria and 1 default message. example:

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);
}

To check if something has not been entered or is blank you can use this:

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

Edit: But from what you commented, you would need a separate if-else statements (like you had in the original post) 1 for each different field you need to check which would test the different conditions for that field.

Upvotes: 1

I&#39;m Geeker
I&#39;m Geeker

Reputation: 4637

Try this

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

Upvotes: 1

sectus
sectus

Reputation: 15464

You need to check non-emptiness instead of existence. So, try to change isset to !empty

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

Upvotes: -1

Related Questions