Home > Net >  HTML form action doesn't find my PHP file
HTML form action doesn't find my PHP file

Time:02-01

I'm building a simple message sending service with HTML, PHP, and MySQL.

guestbook       --- directory
 ㄴ addMessage.php
 ㄴ guestbook.php
...
index.php

at index.php, I included guestbook.php to use functionality in that file.


    <?php include("./guestbook/guestbook.php") ?>

at guestbook.php, I made a simple form to allow writing messages for only logged user, and the data will be send to addMessage.php as I wrote in the code.

<div style="position:absolute; top: 15%; left:25%; width:50%;">

    <form action="addMessage.php" method="post">

        <div >
            <label for="exampleFormControlTextarea1">Write some message!</label>
            <?php
            if (isset($_SESSION['id'])) {

                echo '<textarea  name="message" id="exampleFormControlTextarea1" rows="3" placeholder="~~~~"></textarea>';
                echo '<button type="submit" >write message</button>';

            } else {

                echo '<textarea  id="exampleFormControlTextarea1" rows="3" placeholder="You should login first" readonly></textarea>';
                echo '<button type="submit"  disabled>write message</button>';

            }
            
        ?>
        </div>

    </form>

</div>

and addMessage.php, I put only one line of code to check whether it works.

<?php

    // $conn = mysqli_connect("localhost", "alphatrox", "alphatrox", "cryptogame");

    // for test
    echo $_POST["message"];

?>

But when I send a message, I meet the error message that the addMessage.php(the URL was http://localhost/addMessage.php.) does not exist even I created it in the same directory.

Not Found
The requested URL was not found on this server.

Apache/2.4.52 (Win64) OpenSSL/1.1.1m PHP/8.1.2 Server at localhost Port 80

Can you tell me what I'm doing wrong? I'm facing trouble...

thank you.

CodePudding user response:

Hello i thought you put it in a folder " guestbook ". so your form's action should be action="/guestbook/addMessage.php"

which resolves to http://localhost/guestbook/addMessage.php

The guestbook folder holds the project, always call your urls with reference to the project folder or root directory.

CodePudding user response:

Sometimes as we write code, we do it in a hurry and make basic mistakes. Directory links, comma separators, semicolons and other times using ' ' in place of " " creates small errors that one can sometimes not easily locate.

  •  Tags:  
  • Related