Error appearing before post?

Jemz

xmas tings.
Aug 6, 2013
166
17
Basically, when I refresh my page, the error saying fields are empty appears before I have even clicked submit. How would I resolve this?

PHP:
    <?php
            switch($step) {
            case 1:

            if( isset($_POST['submit'])) {
           
            $db_host   = $_POST['hostname'];
            $db_user   = $_POST['username'];
            $db_pass   = $_POST['password'];
            $db_name   = $_POST['database'];
       
            }
            if (!isset($_POST['hostname']) || !isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['database'])) {
                echo '<div class="error">';
                echo 'You\'ve left some fields <b>empty</b>.';
                echo '</div>';
            } elseif (!$db = @mysqli_connect($db_host, $db_user, $db_pass, $db_name)) {
                echo '<div class="error">';
                echo 'Unable to connect to <b>MySQL</b>. <b>Error Code (2002)</b>';
                echo '</div>';
            } else {
                $conf = file_get_contents( "../inc/conf.php" );
                $conf = str_replace( "replace_h", $db_host, $conf );
                $conf = str_replace( "replace_u", $db_user, $conf );
                $conf = str_replace( "replace_p", $db_pass, $conf );
                $conf = str_replace( "replace_n", $db_name, $conf );
                file_put_contents( "../inc/conf.php", $conf );
            }
           
        ?>
        <form method="post" style="color: rgba(0, 0, 0, 0.43);-webkit-font-smoothing: subpixel-antialiased;">
        <b>Hostname:</b>
        <input type="text" name="hostname" class="input" placeholder="Hostname e.g. localhost"/>
        <br><br>
        <b>Username:</b>
        <input type="text" name="username" class="input" placeholder="Username e.g. root"/>
        <br><br>
        <b>Password:</b>
        <input type="password" name="password" class="input" placeholder="Password" />
        <br><br>
        <b>Database:</b>
        <input type="text" name="database" class="input" placeholder="Database"/>
        </div>
    <div id="main_box" style="background: #FFF;margin-top: 13px;">
        <input type="submit" name="submit" value="Connect" />
        </form>
    <?php
} ?>
 

Disconnect

New Member
Nov 9, 2014
1
0
PHP:
<?php
switch ($step) {
    case 1:

        if (isset($_POST['submit'])) {
            $db_host = $_POST['hostname'];
            $db_user = $_POST['username'];
            $db_pass = $_POST['password'];
            $db_name = $_POST['database'];


            if (!isset($_POST['hostname']) || !isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['database'])) {
                echo '<div class="error">';
                echo 'You\'ve left some fields <b>empty</b>.';
                echo '</div>';
            } elseif (!$db = @mysqli_connect($db_host, $db_user, $db_pass, $db_name)) {
                echo '<div class="error">';
                echo 'Unable to connect to <b>MySQL</b>. <b>Error Code (2002)</b>';
                echo '</div>';
            } else {
                $conf = file_get_contents("../inc/conf.php");
                $conf = str_replace("replace_h", $db_host, $conf);
                $conf = str_replace("replace_u", $db_user, $conf);
                $conf = str_replace("replace_p", $db_pass, $conf);
                $conf = str_replace("replace_n", $db_name, $conf);
                file_put_contents("../inc/conf.php", $conf);
            }
        }
        ?> <form method="post" style="color: rgba(0, 0, 0, 0.43);-webkit-font-smoothing: subpixel-antialiased;">
            <b>Hostname:</b>
            <input type="text" name="hostname" class="input" placeholder="Hostname e.g. localhost"/>
            <br><br>
            <b>Username:</b>
            <input type="text" name="username" class="input" placeholder="Username e.g. root"/>
            <br><br>
            <b>Password:</b>
            <input type="password" name="password" class="input" placeholder="Password" />
            <br><br>
            <b>Database:</b>
            <input type="text" name="database" class="input" placeholder="Database"/>
        </div>
        <div id="main_box" style="background: #FFF;margin-top: 13px;">
            <input type="submit" name="submit" value="Connect" />
        </form>
<?php } ?>
Try that
 

brsy

nah mang
May 12, 2011
1,530
272
It appears that @Disconnect has fixed the problem. Here is what he did:



The reason gave an error before the form was even submitted was because you were checking to see if post values existed before they were submitted. You closed your "was form submitted" if block, prematurely, instead of including the errors inside of that if block. I hope that I'm making sense so that you won't make that mistake again.
 

Users who are viewing this thread

Top