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.

Lal
  • 14,726
  • 4
  • 45
  • 70
Brennan Macaig
  • 313
  • 1
  • 2
  • 9
  • 4
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). In additiona add some error checking to your queries and PHP. – Jay Blanchard Apr 21 '15 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 – Lelio Faieta Apr 21 '15 at 17:24
  • use php `header('Location:'); exit();` instead of `echo "";` – SuperDJ Apr 21 '15 at 17:24
  • @JayBlanchard oh I wasn't aware of that! Thanks for letting me know, I'll convert it all to PDO. However, right now, this is still the situation I have to deal with. I don't want to start the entire conversion process yet, as this is going off to the live server soon.... – Brennan Macaig Apr 21 '15 at 17:24
  • Does the live server support the `mysql_*` API? What version of PHP is it running? – Jay Blanchard Apr 21 '15 at 17:25
  • @JayBlanchard it supports it. It's been running `mysql_*` based stuff until right now when you just recommended that I change it.... – Brennan Macaig Apr 21 '15 at 17:26
  • 3
    Your form elements don't have name attributes - ``/` – Sean Apr 21 '15 at 17:30
  • ^ As Sean said, `$_POST['name']` and `$_POST['blocks']` will never exist because you don't have the name attributes. – Devon Bessemer Apr 21 '15 at 17:33
  • @Sean doesn't seem to fix it.... I cannot figure this out..... – Brennan Macaig Apr 21 '15 at 17:33
  • You need to add the name attributes -> ` – Sean Apr 21 '15 at 17:35
  • 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. – Devon Bessemer Apr 21 '15 at 17:35
  • 1
    Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Apr 21 '15 at 17:35

1 Answers1

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 } ?>
robbmj
  • 16,085
  • 8
  • 38
  • 63