I'm trying to pass javascript variables (name, title, description, type), which have text values stored in them, to a PHP script using AJAX. The PHP script then inserts these variable text values into their appropriate table columns.
However in phpMyAdmin I see that whenever my $.get (I've tried $.post as well) function is called, a new row is created but it simply has null values. Is there any errors syntax-wise to the code below? It is possible to pass variables as objects like I'm doing below right? Because in all the examples I've seen I only see name: 'smith' or type: 1 something similar.
I know it's probably bad practice to name my variables the same as the table column headers, but theoretically it should work, right?
jQuery AJAX
var datavar = {
name: name,
title: title,
description: description,
longitude: longitude,
latitude: latitude,
type: type,
};
$.get('school2.php', datavar, function(data) {
$("#output").html(data);
});
PHP
$sql="INSERT INTO markers (name, title, description, type)
VALUES
('$_GET[name]', '$_GET[title]', '$_GET[description]', '$_GET[type]')";
If I wrote the object literal with array brackets and apostrophes over the object names like this instead:
var datavar = [{
'name': name,
'title': title,
'description': description,
'longitude': longitude,
'latitude': latitude,
'type': type,
}];
I see that in my database table I get just get the default text values 'name', 'title', 'description', etc. instead of null.
name: 'john') instead of variables, the PHP "gets" the text values just fine and inserts them into the database table. The question now is: how do I insert variable values into the object literal to be passed?