Multiple if statements for multiple variables

MyNameIsNotSoCool

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!

I'm Geeker

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related