Search code examples
phphtmlformsvisual-studio-codexampp

Form submission results in Error 404 in PHP and XAMPP


So, I am new to PHP and the main problem is that I was learning and following the lectures and I did exactly what the instructor did but I've got different results and before I show my code, this is my project's directory:

enter image description here

This is my code: The instructor said that if we leave the action = "" blank, the form gets processed by this current file and that is what I want to do.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Form</title>
</head>
<body>
    <h1>PHP Forms</h1>
    
    <form action="get" method="" name="">

        <label>Username:</label>
        <input type = "text" name = "txtUsername" value = ""/>
        <br/><br/>

        <label>Email:</label>
        <input type = "text" name = "txtUserEmail" value = ""/>
        <br/><br/>

        <label>List:</label>
        <select name = "selMember">
            <option value = "Please select">Please select</option>
            <option value = "Joey">Joey</option>
            <option value = "Rachel">Rachel</option>
            <option value = "Chandler">Chandler</option>
            <option value = "Monica">Monica</option>
            <option value = "Phoebe">Phoebe</option>
            <option value = "Ross">Ross</option>
        </select>
        <br/><br/>

        <input type = "submit" value = "Submit" name = "subForm" /> 

    </form>

    <h2>User submitted data:</h2>
    <?php 
        $username = $_GET['txtUsername'];
        $userEmail = $_GET['txtUserEmail'];
        $selectedMember = $_GET['selMember'];
        echo "Username: $username <br/>";
        echo "User's email: $userEmail <br/>";
        echo "User's selected member: $selectedMember <br/>";
    ?>
</body>
</html>

So this is the form: Please ignore the warning notices as the instructor said he will teach how to handle it on the next video. enter image description here

And this is the result after form submission: enter image description here

The problem is that the form gets submitted to this URL: "http://localhost:8080/php_code/get?txtUsername=anyone123&txtUserEmail=anyone%40gmail.com&selMember=Rachel&subForm=Submit"

The URL should be submitted to "http://localhost:8080/php_code/phpForms.php..." instead of "http://localhost:8080/php_code/...".

So, if I manually type in the right URL: "http://localhost:8080/php_code/phpForms.php/get?txtUsername=anyone123&txtUserEmail=anyone%40gmail.com&selMember=Rachel&subForm=Submit", The form submission works just like the instructors: enter image description here

So, how can I fix this problem? Am I placing my files in the wrong directory?


Solution

  • Change your form tag like this.

     <form action="" method="get" name="">
      .......
     </form>