const response = fetch('http://localhost:8888/project/my-script.php', {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({phone_num: 12345678}).toString()
});
PHP
$request_method = $_SERVER['REQUEST_METHOD'];
if ( $request_method == 'POST' ){
echo "This is working!!";
echo($_POST["phone_num"]);
}else {
echo json_encode('No defined function for this method');
}
Solution 2 :
Try using $_REQUEST instead of $_GET.
I know it is lame to use that. But even with ReactJS also, I am facing the same issue and I resolved it with this.
In your above url it seems that you are sending a post request
$request_method = $_SERVER['REQUEST_METHOD'];
if ( $request_method == 'POST' ){
echo "This is working!!";
echo $_GET['phone_num'];
}else {
echo json_encode('No defined function for this method');
}
And if your php function it seems that you are accessing the variable phone_num using the GLOBAL GET array.
Change it to post
$_POST['phone_num'];
Solution 4 :
use this
echo $_POST[‘phone_num’];
Instead of
echo $_GET[‘phone_num’];
Problem :
I have been trying to post data to my PHP script, which works fine for POSTMAN, shows the results correctly. But in while sending the data using AJAX in my html project, the data is not being sent/accepted by the PHP script, hence no show data.
Possible Problem: JSON data type is not working for PHP script, since param is not getting read by $_POST['phone_num']
$request_method = $_SERVER['REQUEST_METHOD'];
if ( $request_method == 'POST' ){
echo "This is working!!";
echo $_GET['phone_num'];
}else {
echo json_encode('No defined function for this method');
}
Tried:
Tried passing the data as { "phone_num" : 12345678 }, not worked
Result: I get This is working!!, but no phone_num which I passed through this code via AJAX
Observation: There is a catch, if we use $_POST['phone_num'] in place of $_GET['phone_num'], then the POSTMAN also doesn’t return any data except This is working!!.
Screenshot:
POSTMAN:
HTML PAGE:
Please help where I am going wrong. That’d be great. Thanks.
Comments
Comment posted by RiggsFolly
Small Point
Comment posted by Alok
To check, if things working fine or not. It won’t make a difference, right?
Comment posted by Baracuda078
You send “phone_num” trough a POST request, in PHP you can fetch that variable then with $_POST[“phone_num”] instead of $_GET, with the $_get you can read variables from the url
Comment posted by RiggsFolly
No wont fix it, but a waste of cpu cycles and memory 🙂
Comment posted by RiggsFolly
echo $_POST['phone_num'];
Comment posted by Alok
By this I get
Comment posted by Nijeesh Joshy
i have edited the answer for decoding the json
Comment posted by Alok
One question, why this doesn’t work in POSTMAN? It doesn’t show up any output, it worked on HTML now. I will mark your answer, just wanted to ask this question.
Comment posted by fetch
@Alok i havnt worked in PHP in long while, i think this is some sort of issue with formatting on the JS side. Working with base
Comment posted by Alok
Thanks Nijeesh, your answer helped me. Thank you so much. I hope people will get benefited with your answer, if that will open again. Hahaha!
Comment posted by Alok
Not working Nikhil. Postman works fine, but for HTML, Same output
Comment posted by Alok
It doesn’t work here. I have mentioned in my CATCH description in my question