Brennan Macaig
Brennan Macaig

Reputation: 323

PHP / HTML form not submitting with MYSQL database

So the below is a snippet of some code of mine.

       <?php
            if (!empty($_POST['name']) && !empty($_POST['blocks'])) {
                $name = mysql_real_escape_string($_POST['name']);
                $blocks = mysql_real_escape_string($_POST['blocks']);


                $registerQuery = mysql_query("INSERT INTO events (Name, Blocks) VALUES('".$name."', '".$blocks."')");

                if ($registerQuery) {
                    echo "<script> window.location.replace('/manager/index?success=addEvent')</script>";
                } else {
                    echo "<script> window.location.replace('/manager/index?failure=addEvent')</script>";
                }
            } else {

        ?>
        <!-- CONTENT CONTAINER -->
        <div class="container">
            <form method="post" action="/manager/addEvent.php" name="registerform" id="registerform">
                <div class="form-group">
                    <label for="name">Event Name</label>
                    <input type="text" class="form-controll" id="name" placeholder="e.g. artblocks">
                </div>
                <div class="form-group" name="blocks" id="blocks">
                    <select class="form-control" for="blocks">
                        <option value="">1</option>
                        <option value="">2</option>
                        <option value="">3</option>
                        <option value="">4</option>
                        <option value="">5</option>
                        <option value="">6</option>
                        <option value="">7</option>
                        <option value="">8</option>
                    </select>
                </div>
                <div class="form-group">
                    <input type="submit" name="register" id="register" class="btn btn-default" value="Submit" />
                </div>
            </form>
            <?php } ?>

When I submit the form on my webpage, it doesn't work at all. It just refreshes the page as if nothing had happened. When I try to use the "BACK" arrow, google chrome warns me that I have submitted information and I'll have to resubmit it. I know it's "submitting", but it's not tossing me back to the main page at all. I have JQuery, and I know that none of the files end with .php that are being referenced, my .htaccess is set up for that. I cannot for the life of me figure out what's wrong here.

Upvotes: 1

Views: 61

Answers (1)

Pravat Kumar Sahoo
Pravat Kumar Sahoo

Reputation: 303

Your Form does not contain a name attribute with the value name. So the if statement will always return false and execute the else part.

Please review the code below.

<?php
    if (isset($_POST['register']) && isset($_POST['name_ex']) && isset($_POST['blocks'])) {
        // REMEMBER: DO NOT USE mysql_* !
        $name = mysql_real_escape_string($_POST['name_ex']);
        $blocks = mysql_real_escape_string($_POST['blocks']);

        $registerQuery = mysql_query("INSERT INTO events (Name, Blocks) VALUES ('".$name."', '".$blocks."')");

        if ($registerQuery) {
            echo "<script> window.location.replace('/manager/index?success=addEvent')</script>";
        } else {
            echo "<script> window.location.replace('/manager/index?failure=addEvent')</script>";

        }
    } else {

?>
    <!-- CONTENT CONTAINER -->
    <div class="container">
    <form method="post" action="/manager/addEvent.php" name="registerform" id="registerform">
        <div class="form-group">
            <label for="name">Event Name</label>
            <input type="text" class="form-controll" id="name_ex"  name= "name_ex" placeholder="e.g. artblocks">
        </div>
        <div class="form-group" name="blocks" id="blocks">
            <select class="form-control" for="blocks" name="blocks" >
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>
        </div>
        <div class="form-group">
            <input type="submit" name="register" id="register" class="btn btn-default" value="Submit" />
        </div>
    </form>
<?php } ?>

Upvotes: 3

Related Questions