0

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 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.

KnightChaser
  • 143
  • 1
  • 9
  • As a simple fix, try `action="guestbook/addMessage.php"`. If that works then look into using `__DIR__` or another way to point to relative paths. Ex, https://stackoverflow.com/a/32537649/296555 – waterloomatt Jan 31 '22 at 17:30
  • Or even better, do `/guestbook/addMessage.php`. Prefixing the path with a `/` will make sure it starts from the web root. If you omit it, then it will try and find the resource relative from the browsers current URL. – M. Eriksson Jan 31 '22 at 17:33
  • On a side note, a common solution to these problems is using the "front controller pattern" which routes _all_ traffic through index.php and then forwards the request to another handler such as guestBook.php. It can be a little complicated to get set up, but once implemented it really helps to get you out of require/include hell. The front controller pattern used with autoloading (ex. https://getcomposer.org/doc/01-basic-usage.md#autoloading) is pure magic. – waterloomatt Jan 31 '22 at 17:34
  • Thank you for everyone helping me by comments. I solved the problem in your favor. Also, I'll acquaint the prefix `/`... – KnightChaser Jan 31 '22 at 17:37

2 Answers2

0

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.

Ande Caleb
  • 1,163
  • 1
  • 14
  • 35
0

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.

<form action="guestbook/addMessage.php" method="POST"> 
Saman Salehi
  • 1,004
  • 1
  • 12
  • 19