0

I have a form with one text input field:

index.html

<form method="post" action="comment.php">
  <a href="javascript:void(0)">
    <div class="avatar" style="background:url('img/user4561.jpg') center/cover"></div>
  </a>
  <input name="comm" type="text"/>
  <input type="submit" value="submit"/>
</form>

And here is comment.php in the same directory

<?php
  echo $_POST["comm"];
?>

Now the most incredible event. The form data does not get submitted! I get an error:

Notice: Undefined index: comm in 192.168.0.1/comment.php on line 2

I changed comment.php like this:

<?php
  if(isset($_POST["comm"])) {
    echo "It is set";
  } else {
    echo "It is not set";
  }
?>

and I get:

It is not set

So, I copied the HTML code of the form into another blank webpage. I also changed the action attribute of the form to "192.168.0.1/comment.php". Now I get

It is set

I also tried using GET instead of POST, but I am again in the same conflict.

EDIT: I removed all other files and code, except for the form and its script. The problem persists. You can now read and modify the source code. Go to files.000webhost.com. Use chatstack as the website name and 6RxOkuJ1CIkbNqxKUMGr as the password.

EDIT: I modified the code for the form above to exactly match that in the above given link. I noticed that removing the link from inside the form solves the problem. I have already done this, but this is so weird. According to this post it is totally fine to have other elements inside a <form> element. There are also other parts of my website where I have <div> elements inside a form element and the form is working fine.

What is this? Is it the server or something else?

I am pretty sure the information provided here is not enough. Feel free to ask for any additional information. There is just too much information to write in a post, and I don't know which part of it is relevant.

Wais Kamal
  • 5,858
  • 2
  • 17
  • 36
  • Which file has the `
    ` in it? Is it `comment.php` or something else?
    – Phil Oct 02 '18 at 00:09
  • The file is an **index.html** in the same directory as the form. – Wais Kamal Oct 02 '18 at 00:13
  • I am using WAMP server. I also tried [000 Web Host](https://www.000webhost.com) and the same problem exists. – Wais Kamal Oct 02 '18 at 00:18
  • Oh sorry for that. What do you mean by get to comment.php? I can type "192.168.0.1/comment.php" in the address bar, but I get an error message, or, in the second case, "It is not set" echoed. – Wais Kamal Oct 02 '18 at 00:21
  • Yes, it seems that the only way to reach it is via the form. I am not an expert in PHP so I don't know if you could send a POST request in another way. If you could then that is another way to reach it. – Wais Kamal Oct 02 '18 at 00:24
  • I know the difference between the two, but this is not the problem here. I will try to submit the form using get and update comments.php accordingly, then I will edit the question with the results. – Wais Kamal Oct 02 '18 at 00:28
  • Are you actually submitting the form before checking if `$_POST` is set? – Havenard Oct 02 '18 at 00:38
  • I know that, @Phil, and yes, first I submit the form to comments.php, then the server executes the PHP code that checks for the validity of `$_POST`. – Wais Kamal Oct 02 '18 at 00:40
  • Your question now needs serious clarification. The only workflow of your application should be 1) Open `http://localhost/index.html` (or whatever hostname / IP you prefer). 2) Submit the form by clicking the submit button or hitting the `Enter` key while typing in the text field. The URL should then change to `http://localhost/comment.php` and you should see _"It is set"_. If you manually type `http://localhost/comment.php` into your browser, that is a GET request and **will not work**. – Phil Oct 02 '18 at 00:44
  • In reply to the workflow comment, that is exactly what is happening. I am so confused because I saw many questions on Stack Overflow complaining about the same problem, but in all cases it was the form not getting submitted. Here the form is getting submitted from the same directory as the script but it is not being recognized/received. However, it is getting received when it is sent from a different directory. Is there something like this in PHP settings? – Wais Kamal Oct 02 '18 at 00:48
  • I am not sending both a GET and a POST request. I am sending a POST request, but I also tried sending a GET request **instead** of the post request. – Wais Kamal Oct 02 '18 at 00:50
  • Sorry for not mentioning that. Everything you described is what exactly is happening, except the message. I am getting "It is not set". – Wais Kamal Oct 02 '18 at 00:56
  • 1
    you said the issue happens on a web host , so can you provide a link to show this. –  Oct 02 '18 at 01:05
  • @WaisKamal, I have no issue running your code (source file) https://firanalda.com/test.zip – Adlan Arif Zakaria Oct 02 '18 at 02:27
  • I added a link from which you can read and modify the source code. – Wais Kamal Oct 02 '18 at 20:57

1 Answers1

2

Open your dev-console in browser. Go to the network tab. Now you fill in your form and submit. After your comment.php is loaded, you check for the very first row in the network tab (should be the comment.php html document). When you click on that request it should display you, whether the request was GET or POST and also what data were sent to this document.

You can also try at comment.php var_dump($_REQUEST); to see what data were sent and how it can be accessed.

By this you can see if server all over receives any data. If yes, then you go for an other server like apache2 for testing purpose to look if bug is fixed.

That's how I would to that, but I suspect that by editing your code for the public, you accidentally withheld some information that would solve this simple problem. It's very unlikely that web-servers like WAMP had passed the testing environment before publishing a new version allthough no data could be procesed by server

saladzic
  • 56
  • 6
  • _"go for an other server like apache2"_ <- OP says they're using WAMP so they already have Apache – Phil Oct 02 '18 at 02:11
  • You can buy a Mercedes Benz E63 AMG or a customized one from Mansory. Mansory cars could have problems with breaks therefore a classical AMG has no problems with it. Anyway it's completely unlikely that the problem of OP comes from this web-server version. – saladzic Oct 02 '18 at 02:22
  • Oh, I completely agree. I get the feeling OP has probably spotted their _"typo"_ (I like how you put it better) and abandoned this post – Phil Oct 02 '18 at 02:24
  • By the way I didn't edit my code for the public. This is the initial code I am using for testing before implementation. I will try to share the actual server code and give users the right to edit. I also did the inspection in the dev tools network tab. The correct message is being sent, the method is POST and the status is 200. – Wais Kamal Oct 02 '18 at 06:43
  • @Phil no, actually the OP was sleeping :) – Wais Kamal Oct 02 '18 at 06:47
  • @WaisKamal sleep is for the weak ;-) – Phil Oct 02 '18 at 06:48
  • Bro it was 3 AM :) – Wais Kamal Oct 02 '18 at 06:49
  • @Wais Kamal my ideas are going to the end and I don't know if it could be an issue, but you could check if both files have the UTF-8 charset. Otherwise it's definetly not your mistake and try your source code on a freshly new installed debian with apache or so. – saladzic Oct 02 '18 at 11:58
  • I added the link for the source files. I am not a PHP expert so I think it would be easier for experts to debug the problem if they can read and modify the source code. – Wais Kamal Oct 02 '18 at 20:57