How to use post in php? [SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to use post in php? [SOLVED]

I have virtual web (centos 6.10, php version 5+) and I'm trying to make request with post, however array $_POST is empty, Somebody know what could solve this?

7th Sep 2020, 5:54 PM
nicolas turek
nicolas turek - avatar
18 Answers
+ 1
With forms(application/x-www-form-urlencoded), it should work if method="post". However $_POST does not work for other Content-Type. You have to manually read 'php://input' stream. Can you share the php code? Also verify that your form's method is POST.
7th Sep 2020, 6:22 PM
Ore
Ore - avatar
+ 1
Are you sure there is an input field with name="user" in the submitted form?
7th Sep 2020, 6:27 PM
Ore
Ore - avatar
+ 1
Try to manually read php://input stream. It should contain the said data.
8th Sep 2020, 9:09 AM
Ore
Ore - avatar
+ 1
Hmm 🤔
8th Sep 2020, 5:40 PM
Ore
Ore - avatar
+ 1
nicolas turek I know it is JS. I was talking about your PHP. According to you, 'files' and 'files/' both point to the same route but 'files' (without slash) does not send the posted data. To me, that is a flaw in the routing system of your website. You should either return a 404 response or trim the extra slash.
8th Sep 2020, 9:09 PM
Ore
Ore - avatar
0
How are you making the request?
7th Sep 2020, 6:07 PM
Ore
Ore - avatar
0
I tried form and xmlhttprequest (with get it works)
7th Sep 2020, 6:10 PM
nicolas turek
nicolas turek - avatar
0
Versions: Php 5.3.3, Apache 2.2.15
7th Sep 2020, 6:15 PM
nicolas turek
nicolas turek - avatar
0
Php: <?php if(isset($_POST["user"]))echo "ok"; else echo "ko"; ?>
7th Sep 2020, 6:24 PM
nicolas turek
nicolas turek - avatar
0
I tried it with get
7th Sep 2020, 6:28 PM
nicolas turek
nicolas turek - avatar
0
Problem is in post, not way I get things to there, I think it could be something with server
7th Sep 2020, 6:29 PM
nicolas turek
nicolas turek - avatar
0
Thanks for this, no solution worked sadenly
7th Sep 2020, 6:48 PM
nicolas turek
nicolas turek - avatar
0
Ok, I find solution by myself - it's really stupid: there has to be / at the end of url :D
8th Sep 2020, 4:44 PM
nicolas turek
nicolas turek - avatar
0
My first request open part was (not working): req.open("POST", "files", true); Working: req.open("POST", "files/", true);
8th Sep 2020, 5:43 PM
nicolas turek
nicolas turek - avatar
0
nicolas turek Are you using a router script or relying on file system based routing?
8th Sep 2020, 5:47 PM
Ore
Ore - avatar
0
No, that's js, just I'm lazy writing whole script, only thing you need to know, that req is instance object of xmlhttprequest
8th Sep 2020, 5:50 PM
nicolas turek
nicolas turek - avatar
0
The thing I'm doing include json files, so I don't think it's possible to do it with sololearn
8th Sep 2020, 5:51 PM
nicolas turek
nicolas turek - avatar