1

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.

12
  • 4
    Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO. In additiona add some error checking to your queries and PHP. Commented Apr 21, 2015 at 17:23
  • I quote @JayBlanchard and add: can you post the first part of your code. The issue is probably on the part you didn't post Commented Apr 21, 2015 at 17:24
  • 3
    Your form elements don't have name attributes - <input type="text" class="form-controll" id="name" placeholder="e.g. artblocks">/<select class="form-control" for="blocks"> Commented Apr 21, 2015 at 17:30
  • 2
    Sounds like you need a way to debug. var_dump or print_r on your $_POST. This type of comment: I don't want to start the entire conversion process yet, as this is going off to the live server soon. worries me. You should be addressing deprecated and security issues before it goes to a live server, not after. Commented Apr 21, 2015 at 17:35
  • 1
    Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); Commented Apr 21, 2015 at 17:35

1 Answer 1

3

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

1 Comment

Thanks! Fixed your first name to name_ex as it should be according to your code.

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.