0

Okay so my website is setup like /index.php?page=PAGE.

I have a form on my contact page, but when I submit the POST form it strips the URL to /index.php?

Why is that? I haven't touched PHP in months, but I don't remember this ever being an issue.

Here is my code:

                                    <form action="" method="POST">
    <?php
        if (isset($_POST['contactBtn'])) {
            $errors = array();
            $name = $_POST['ContactInputName'];
            $email = $_POST['ContactInputEmail'];
            $message = $_POST['ContactInputMessage'];

            if(empty($name)) {
                $errors[] = 'You did not enter a name.';
            }
            if(empty($email)) {
                $errors[] = 'You did not enter an email.';
            }
            if(empty($message)) {
                $errors[] = 'You did not enter a message.';
            } 
            if(strlen($message) < 4 || strlen($message) > 4096) {
                $errors[] = 'Message length must be between 4 and 4096 characters long.';
            }

            if(empty($errors)) {


                $Database->selectPrepare("INSERT INTO  `contact` (`ID` ,`name` ,`email` ,`message` ,`time`, `IP`) VALUES (NULL ,  :name,  :email,  :message,  :time, :IP);",
                    array(':name' => $name, ':email' => $email, ':message' => $message, ':time' => time(), ':IP' => $_SERVER["HTTP_CF_CONNECTING_IP"]));
                echo '  <div class="alert alert-success">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <h4>Success!</h4>
                            Your message has been recieved and we will get back to you as soon as possible!
                        </div>';

            } else {
                echo '
                <div class="alert alert-danger">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    <h4>ERROR!</h4>';
                    foreach($errors as $error) {
                        echo ' <p>'.$error.'</p>';
                    }
                    echo '</div>';
            }

        }
    ?>
    <div class="form-group">
        <label for="ContactInputName">Name</label>
        <input type="text" class="form-control" id="ContactInputName" placeholder="John Smith">
    </div>
    <div class="form-group">
        <label for="ContactInputEmail">Email address</label>
        <input type="email" class="form-control" id="ContactInputEmail" placeholder="John@smith.com">
    </div>
    <div class="form-group">
        <label for="ContactInputMessage">Message</label>
        <textarea class="form-control" id="ContactInputMessage" rows="4"></textarea>
    </div>

  <button type="submit" id="contactBtn" class="btn btn-ar btn-primary">Submit</button>
</form>
esote
  • 763
  • 10
  • 24
Austin Willey
  • 39
  • 1
  • 3

3 Answers3

1

It wouldn't. If the request URI was /index.php?page=PAGE, with an empty action attribute on the form, the browser would send the POST request to /index.php?page=PAGE, it wouldn't alter it. I suspect that whatever difference in behavior you're observing has to do with your web server's rewrite rules or server-specific configuration. This isn't something PHP would do.

Sherif
  • 11,196
  • 3
  • 26
  • 54
  • That makes sense, but I can't figure out why it isn't working. Is there something I can do to workaround this? – Austin Willey Sep 26 '16 at 01:56
  • Well, first you have to figure out what's causing the problem. Start by checking your web server's access/error logs and rewrite rules. That's most likely the best place to start since it's the first point of entry where the request URI can be modified on the back end. – Sherif Sep 26 '16 at 01:57
  • (ie: take a look at your .htaccess file) – Damien Sep 26 '16 at 01:58
0

This was because of cloudflare. It had cached my previous form. I checked the webpage source and noticed it had old code.

Thanks guys!

Austin Willey
  • 39
  • 1
  • 3
-2

EDIT: I misunderstood the question, this isn't the answer to OP's question. But if you're interested in learning a little bit more about GET, POST, and HTTP in general, read on.

The TL;DR is:

  1. When you use GET to send data from the client to the server, you send it in the URL.

  2. When you use POST to send data from the client to the server, you send it in the body of the HTTP request itself.


HTTP has different methods of passing data back and forth between the client (your browser) and the server (where your PHP code runs) which include GET and POST.

This post is a good explanation of the difference between GET and POST.

Here's a W3Schools explaination as well.

And a tutorial on all HTTP methods

Community
  • 1
  • 1
Hebron George
  • 269
  • 1
  • 6
  • 21