0

I am trying to make a page with form that allows people to submit requests to become a volunteer for the non-profit organization I am making a website for. These requests should be then logged into a database called "volunteers" on phpmyadmin. As for servers I am trying to work with Xampp with Apache and MySQL. When testing my page however, I simply get the error: Cannot Post /Connection.php. At some point of time in my trouble shooting, it simply displayed a page of the php code instead of the cannot post message while still not sending the data to phpmyadmin, however now it's back to the old cannot post stuff.

For reference, my two files here, the html file, "Register Volunteer.html" and the php file, "Connection.php" are within the same folder, which is within htdocs of Xampp where it is supposed to be. The server is active too and ran on administer permissions. The php file is heavily based on a college homework assignment I did a year ago. I feel like I've been bashing my head against a wall with for too long. Is there any flaws in my code that spring to mind to any experts out there? Do I perhaps need another file?

Here is "Register Volunteer.html"...

<!DOCTYPE html>
<html>
 <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Soaring Dreams - Home</title>
        <link rel="stylesheet" href="main.css">
        </head>
    <body>
        <div id="bannerimage"></div>
        <header class = "aligncenter"></header>
        <hr>
        <nav>
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="About%20Us.html">About Us</a></li>
                <li><a href="Gallery.html">Gallery</a></li>
                <li><a href="Our%20Volunteers.html">Our Volunteers</a></li>
                <li><a href="Custom%20Goods.html">Custom Goods</a></li>
               
                
            </ul>
        </nav>
        <h2><i>Volunteer Registration</i></h2>
        <form action="/Connection.php" method="post">
            <fieldset>
        
        <label> First name </label>   
        <input type="text" id="firstName" name="firstName" placeholder= "Firstname" size="20" required />
        <br>
        <label> Last name </label>   
        <input type="text" id="lastName" name="lastName" placeholder= "Lastname" size="20" required />
        <br>
        <label> Date of Birth </label>   
        <input type="date" id="age" name="age"   required />
        <br>
        <label> Phone Number </label>   
        <input type="text" id="phoneNumber" name="phoneNumber" placeholder= "+1(123)123-1234" size="20" required />
        <br>
        <label> Email </label>   
        <input type="text" id="email" name="email" placeholder= "Something@blah.com" size="30" required />
        <br>
        <br>
        <label>Why do you want to volunteer?</label>
        <textarea rows="6" cols="100" id="reason" name="reason" placeholder= "Add your reason" size="600"  required></textarea>
        <br>
        <br>
        < type="submit" class="registerbtn">Register</>
                </fieldset>
            </form>
    </body>
</html>

And here is "Connection.php"...

<?php  
       $servername = "localhost";  
       $username = "root";  
       $password = "examplePW";
        $dbname = "volunteers";  
       $conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}
    echo "Connected successfully. <br>";
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $age = $_POST['age'];
    $phoneNumber = $_POST['phoneNumber'];
    $email = $_POST['email'];
    $reason = $_POST['reason'];
        

    $sql = "INSERT INTO volunteerrequest(firstName, lastName, age, phoneNUmber, email, reason) VALUES" . "('$firstName', '$lastName', '$age', '$phoneNumber', '$email', '$reason')";
    echo "Running SQL statement - <br>" . $sql . "<br>";

    if($conn->query($sql) == TRUE)
    {
        echo "Request Sent <br>";
    }
    else{
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
        
        
?>

Again, any sort of help or guidance would be immensely appreciated.

error page

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
  • Here's post with similar issue [Cannot POST](https://stackoverflow.com/questions/26056150/cannot-post-contact-php) – Bevin Pavithran Feb 10 '21 at 21:05
  • I see some misspelling here: https://i.imgur.com/K2xzHy7.png but that's probably not the problem. Maybe adding `enctype="multipart/form-data"` into the `
    ` might help? Also if `Connection.php` is in same folder get rid of the `/` like this: `action="Connection.php"`. That might be why.
    – Crimin4L Feb 10 '21 at 21:22
  • 1
    yea, the / isn't exactly the issue, I tried putting that there as a means to troubleshoot, but got the same result anyways... I'll try that enctype EDIT: nope. no dice – Elliott Pascoe Feb 10 '21 at 22:08
  • Can you post a picture of the error? – Daniel Gitahi Feb 11 '21 at 15:29
  • I just added a link to the picture of the error page. Later today I'm going to try to uninstall and reinstall Xampp to see if that works, unless you have a better option. – Elliott Pascoe Feb 11 '21 at 16:09
  • 1
    "Cannot post" is not an error message that any basic PHP would issue - something else is going on. At the top of your PHP script, `error_reporting(E_ALL); ini_set('display_errors', 1);` - always when developing and testing code. If you saw your PHP code rendered to the browser, that usually suggests it was accessed like `file:///path/to/Connection.php` instead of `http://localhost/Connection.php` OR it means a serious misconfiguration in Apache. – Michael Berkowski Feb 11 '21 at 16:19
  • Please also review [How can I prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) - your `INSERT` statement is quite vulnerable to tampering, and likely to result in your database getting filled up with spam in short order. You should replace `$conn->query()` with `$conn->prepare()` and `execute()`, replacing all the query `$` variables with `?` bound parameters as in the examples. – Michael Berkowski Feb 11 '21 at 16:22
  • Your data isn't stored by phpMyAdmin, it's in a database such as MySQL or MariaDB. You can use phpMyAdmin as a tool to view the data stored in the database, as well as perform administrative tasks on the server, but to be precise the data isn't stored in phpMyAdmin itself. – Isaac Bennetch Feb 11 '21 at 18:36
  • Hmm. I see. That is quite odd. Thanks for the insight Micheal. I wonder how I can resolve that path access issue. Is it perhaps because I'm testing it with the Brackets editor's live preview? – Elliott Pascoe Feb 11 '21 at 18:42
  • @ElliottPascoe I've tested it on my PC and it appears to work just fine. It would be more helpful if you enabled error reporting to enable you to post a more helpful/meaningful error message. – steven7mwesigwa Feb 11 '21 at 19:15
  • `Connected successfully. Running SQL statement - INSERT INTO volunteerrequest(firstName, lastName, age, phoneNUmber, email, reason) VALUES('Docvf', 'ZASX', '2021-02-23', '79585980090', 'steveikats1398756@gmail.com', 'kvyuyp') Request Sent` – steven7mwesigwa Feb 11 '21 at 19:15
  • Your submit button lucks tags, though I'm sure that's not the issue you're facing. `< type="submit" class="registerbtn">Register>` – steven7mwesigwa Feb 11 '21 at 19:19
  • @steven7mwesigwa well that's some good news at least – Elliott Pascoe Feb 11 '21 at 23:36
  • @steven7mwesigwa So basically, if I run the html page with Brackets prieview on chrome, I get Cannot Post. If I run it directly by opening it on Chrome, It just displays the code. How exactly are you connecting it to the server? I have Xammp apache and mySQL active, as well as the file in the htdocs folder. There must be an important step I am missing no? – Elliott Pascoe Feb 11 '21 at 23:46

1 Answers1

0

I found my issue! I thought I changed the port in the apache config file from 80 to 81, but apparently I didn't save that change. I also figured out how to properly test the website with the server with the right URL.