0

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.

3
  • I found that it is in fact my variables in the object literal causing the problem. If I simply placed strings (like 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? Commented Mar 25, 2013 at 1:59
  • 1
    Where does these variables come from? Commented Mar 25, 2013 at 2:09
  • Oh, I fixed it. It turns out the methods (regarding the javascript object literal and the php INSERT array)I was originally using and also kpsuperplane's was valid. The problem was that the variables (name, title, description) were getting values from a javascript onchange event that happened after a "submit" button click. I moved the onchange block of code outside the click function and the variables could properly get their values. Thanks for interest and the help you two! Commented Mar 25, 2013 at 2:23

1 Answer 1

1

$_GET[x] is incorrect, it should be $_GET['x']. try this:

$sql="INSERT INTO markers (name, title, description, type)
VALUES
('".$_GET['name']."', '".$_GET['title']."', '".$_GET['description']."', '".$_GET['type']."')";

Also, as a side note you probably want to sanitize your database inputs in order to prevent SQL injections.

Sign up to request clarification or add additional context in comments.

9 Comments

$_GET[x] is incorrect, it should be $_GET['x'] nope. see codepad.org/xlqrY4iY
This is also what was confusing me :\
@Gnuey does it work if you directly run the SQL from your database management software with predefined value like such? INSERT INTO markers (name, title, description, type) VALUES ('w','x','y','z')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.