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");

  <?
the_boy_za
  • 287
  • 1
  • 8
  • 21
  • *"`$vraag ="INSERT INTO kijken (merk) VALUES ='$getcontent' ";`"* You might want to read this: http://en.wikipedia.org/wiki/SQL_injection And this: http://xkcd.com/327/ – T.J. Crowder Mar 30 '12 at 11:04
  • 1
    Why you want to send the selected item in JSON format? – Milindu Sanoj Kumarage Mar 30 '12 at 11:16
  • i was advice by someone that if i wanted to send multiple data @once i should use JSON instead. – the_boy_za Mar 30 '12 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); – Sakthi Mar 30 '12 at 15:03

3 Answers3

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.

Stefan
  • 3,850
  • 2
  • 26
  • 39
  • I've tried both solutions but i still end-up with empty columns in my DB – the_boy_za Mar 30 '12 at 12:06
  • Did you setup a SQL connection in your php script? – Stefan Mar 30 '12 at 12:12
  • @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. – Stefan Mar 30 '12 at 12:32
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?).

Tei
  • 1,400
  • 8
  • 6
  • He also has `true` in json_decode()... so he is NOT trying to use it as a string...? – Stefan Mar 30 '12 at 11:06
  • @Stefan Ooops... true, is not a object. But is a array, so is still not the string he needed. – Tei Mar 30 '12 at 11:12
  • Yes, because $getcontent->{'Brand'} won't work on an associative array, not in your code either. – Stefan Mar 30 '12 at 11:21
  • @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. – the_boy_za Mar 30 '12 at 11:50
  • Well, start by printing what you receive. var_export($getcontent), var_export($data_s) and so on. Figure out where your data is lost. – Tei Mar 30 '12 at 11:52
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'];
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • 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? – the_boy_za Mar 30 '12 at 12:23
  • 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 – charlietfl Mar 30 '12 at 12:32