<script>
document.getElementById('type').addEventListener('submit', insertType);
function insertType(e) {
e.preventDefault();
var type = document.getElementById('type_to_add').value;
var xhr = new XMLLHttpRequest();
xhr.openn("GET","insert_type.php?Type="+type, true);
xhr.onload = function() {
console.log('Submitted');
}
xhr.send();
}
</script>
the error points to the type variable, but I dont see how it can be null
Im trying to insert the input into a mysql table
I thought that is what this part did
document.getElementById('type').addEventListener('submit', insertType);
function insertType(e) {
e.preventDefault();
var type = document.getElementById('type_to_add').value;
var xhr = new XMLHttpRequest();
xhr.open("GET","insert_type.php?Type="+type, true);
xhr.onload = function() {
console.log('Submitted');
}
xhr.send();
}
I really wasn’t sure if I should use GET or POST
, I thought the opnly difference was using GET would put it in the url and POST did not, is that not the cas e?
heres the php file
But if the receiving page is looking for the data in the POST fields, and not the URL… then sending it on the URL will result in it not seeing anything.
You execute your query, but you don’t appear to be checking to see if there were any errors in your query.
What happens if you echo $mysql->error(); after your execute? (Check the Response to your request by looking at the details of that request in your network panel)
If your script inserts into the database, POST or PUT is the way to go; GET requests are always assumed to have no side-effects.
You’re missing the closing parenthesis after the if condition… are you not using a a text editor that highlights such errors? Basically any proper code editor will recognise at least basic syntax errors – personally I’d suggest VSCode which is free and leaves little to be desired IMHO.
The request headers show a content length of zero. That combined with the general request method being POST, indicates something odd is happening. Because, the JavaScript code uses GET instead.
It seems that something else made that POST request, instead. The GET request from the JavaScript code is not what you are looking at in the headers there.