Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HTML form action doesn't find my PHP file

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

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

                echo '<textarea class="form-control" name="message" id="exampleFormControlTextarea1" rows="3" placeholder="~~~~"></textarea>';
                echo '<button type="submit" class="btn btn-primary">write message</button>';

            } else {

                echo '<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="You should login first" readonly></textarea>';
                echo '<button type="submit" class="btn btn-primary" 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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading