I’m trying to use fetch instead of xmlhttprequest but fetch isn’t working the way the comparable ajax system does, or, at least, I can’t get it to work the same way. I have the following code:
addAction("pressBTN", "click", function() {
const data = new FormData();
data.append('dset1', theElement("dataINP1").value);
data.append('dset2', theElement("dataINP2").value);
/* for (var pair of data.entries()) {
console.log(pair[0]+ ', '+ pair[1]);
} */
fetch('./files/php/scripts/dataEntery.php', {
method: 'POST',
body: data
})
.then(response =>
if (!response.ok) {
throw new Error('Unknown Error #4: nice way to mess it up');
} else {
console.log(response);
}
}).catch((err) => {
console.log(err);
});
}, true);
Note that theElement and addAction just keep me from having to type out
document.getElementById
and
document.addEventListener
over and over.
This process returns and ok response. Cool! Except nothing in the php file gets ran at all.
With the previous system xmlhttprequest, it called the php file, ran its contents and returned. In that PHP, I could check the values against a database and you know do server stuff.
I’ve tried moving the php file to the same location as the javascript file. Nothing. I made sure the data values were formed correctly which is still there in the commented code.
I’ve tried json because every tutorial out there shows json. It still hasn’t worked. Not even the simplest command like
<?php
print_r($_POST);
echo “balls!”;
?>
works in the php file. I honestly don’t know why. It looks like the code should work to me. Perhaps I am missing something here.
Except for the HTML that sets up two inputs as text and a button there is no other code in the project. This LOOKS like it shoukd work. The console returns the following when it logs the response:
body: ReadableStream
statusText: “OK”
json: (function)
redirected: false
text: (function)
status: 200
bodyUsed: false
url: “goodurlpath/files/php/scripts/dataentry.php”
type: “basic”
blob: (function)
arrayBuffer: (function)
formData: (function)
headers: Headers
value: (function)
keys: (function)
delete: (function)
forEach: (function)
entries: (function)
get: (function)
append: (function)
set: (function)
has: (function)
clone: (function)
ok: true
I’m wondering if the PHP is even supposed to run with fetch!?
Any help with why the php isn’t running here would be appreciated.