0

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!

2
  • please paste ur code fully Commented Dec 22, 2014 at 10:24
  • Done! I've added to <form> code! Commented Dec 22, 2014 at 10:27

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Okay this works! But how would I add multiple of these for different $_POST values?
do u have any example of multiple ?
1

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.

2 Comments

The problem with the first code is that I'd need multiple else statements because I would need to add different strings to different inputs, how would I do this? Thanks!:)
I assume that you mean if diffrent fields are blank? in that case you will need a diffrent if statement for each input field
0

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

Comments

-1

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

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

9 Comments

so your checking if its not empty?
@NoLiver92, fixed :^ ). But yes. if it contains data means not empty.
empty() checks if something is empty. the "!" is the opposite. eg is and isnot, set and not set. your "!empty" check to see if its not empty, aka has a value. you do not check if its empty. your statement does the same as isset()
@NoLiver92, are you sure?
@NoLiver92 You must be kidding. Whether you don’t understand the difference, re-read the article you provided yourself (and try to execute the code I provided.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.