0

I've created this form with a jQuery autocomplete function. The selected brand from the autocomplete list needs to get sent to a PHP file using $.ajax function. My code doesn't seem to work but i can't find the error. I don't know why the data isn't getting inserted into MYSQL database. Here is my code:

JQUERY:

           $(document).ready(function() {

    $("#autocomplete").autocomplete({
        minLength: 2
    });

    $("#autocomplete").autocomplete({

        source: ["Adidas", "Airforce", "Alpha Industries", "Asics", "Bikkemberg", "Birkenstock", "Bjorn Borg", "Brunotti", "Calvin Klein", "Cars Jeans", "Chanel", "Chasin", "Diesel", "Dior", "DKNY", "Dolce &  Gabbana"]

    });

    $("#add-brand").click(function() {

        var merk = $("#autocomplete").val();

        $("#selected-brands").append(" <a class=\"deletemerk\" href=\"#\">" + merk + "</a>");

                //Add your parameters here        
                var param = JSON.stringify({
                    Brand: merk
                });

                $.ajax({
                    type: "POST",
                    async: true,
                    url: "scripttohandlejson.php",
                    contentType: "application/json",
                    data: param,
                    dataType: "json",
                    success: function (good){
                       //handle success

                       alert(good)
                    },
                    failure: function (bad){
                       //handle any errors

                       alert(bad)

                    }
                });


        return false;

    });

});

PHP FILE: scripttohandlejson.php

  <?PHP

     $getcontent = json_decode($json, true);

     $getcontent->{'Brand'};

     $vraag = "INSERT INTO kijken (merk) VALUES ('$data_s')";

     $voerin = mysql_query($vraag) or die("couldnt put into db");

  <?
4
  • "$vraag ="INSERT INTO kijken (merk) VALUES ='$getcontent' ";" You might want to read this: en.wikipedia.org/wiki/SQL_injection And this: xkcd.com/327 Commented Mar 30, 2012 at 11:04
  • 1
    Why you want to send the selected item in JSON format? Commented Mar 30, 2012 at 11:16
  • i was advice by someone that if i wanted to send multiple data @once i should use JSON instead. Commented Mar 30, 2012 at 12:22
  • In the php script, you used json_decode function to convert into array. In the next line, your trying to get access like object. that may be a pblm. Better, print the value in every statement and try to track it by using firebug. If you have clarification still, please paste the ajax reponse and also try to debug by setting the error_reporting(E_ALL); and then ini_set(display_error,2); Commented Mar 30, 2012 at 15:03

3 Answers 3

1
$arr_content = json_decode($json, true);

$brand = $arr_content["Brand"];

$vraag = "INSERT INTO kijken (merk) VALUES ('" . $brand . "')";

EDIT:

THE PROBLEM: In your example, your json_decode returns an associative array because you have the parameter 'true' in the function call. But in the next line you are attempting to use the result like an object by trying to access 'it's 'BRAND' property.

All you have to do is to remove true from the json_decode function call, or alternatively use my code above.

NOTE: Edited SQL statement too.

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

3 Comments

I've tried both solutions but i still end-up with empty columns in my DB
Did you setup a SQL connection in your php script?
@the_boy_za: Your problem now is probably with the database insertion, not with the value you receive. But check that value by putting echo $getcontent->{'Brand'}; or echo $brand; in your php script, then in Firefox->Tools->Web Developer->Firebug go to XHR tab. Then run your application and select a value, and check the response in the Firebug console.
0

json_decode returns a object, not a string, and you are tryiing to use it as a string, so you probably are inserting something like "Object()" in the database.

maybe you can do $data_s = mysql_escape_string($getcontent['Brand']); then use $data_s instead.

PHP FILE: scripttohandlejson.php

  <?PHP

     $getcontent = json_decode($json, true);

     $data_s = mysql_escape_string($getcontent['Brand']);

     $vraag ="INSERT INTO kijken (merk) VALUES ='$data_s' ";

     $voerin = mysql_query($vraag) or die("couldnt put into db");

  ?>

I have not checked for other possible errors (like.. if this SQL valid?).

5 Comments

He also has true in json_decode()... so he is NOT trying to use it as a string...?
@Stefan Ooops... true, is not a object. But is a array, so is still not the string he needed.
Yes, because $getcontent->{'Brand'} won't work on an associative array, not in your code either.
@Tei I've changed the mysql_query part code wasn't valid indeed. But code is still not working. When executed I receive a empty column in my DB.
Well, start by printing what you receive. var_export($getcontent), var_export($data_s) and so on. Figure out where your data is lost.
0

There is no need to send the data as JSON.

Change this in JS:

   //Add your parameters here        
   var param = JSON.stringify({
          Brand: merk
   });

TO:

   var param ={Brand: merk};

In php will receive it with:

 $brand= $_POST['Brand'];

2 Comments

I've tried this solutions but i still end-up with empty columns in my DB. I don't know if this has something to do with utf-8?
is request being made ? can look at full request in console. Try sending back the post as response. echo $_POST['Brand'] and see if it gets returned to page in ajax. Add some mysql error handling too, and that can be sent back in ajax also

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.