0

Updated Problem: print_r($_POST); in the php file outputs after I type something in the box.

Array
(
    [q] => running
)

print json_encode($jsonArray); however is printing []

The php which queries Interest table for the bound parameter

    $interestValue = $_POST['interestVal']; //////
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
    $sth = $dbh->prepare(
'SELECT interestID, interestVal FROM Interest WHERE interestVal = ?');
    $sth->bindParam(1, $interestValue);
    $sth->execute();
    $jsonArray = array();
    while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
     $jsonArray[] = array(
      'ID' => $result['interestID'], 
          'Value' => $result['interestVal']); 
    }

    print_r($_POST);

    print json_encode($jsonArray); // json encode that array 

HTML

<input id="interest" name="interest" value="What are your interests?" />

JS

//Get interests
        $(document).ready(function() {
            $("input#interest").tokenInput("../../src/php/registration/interest/getInterest.php");
        });

The JS of the framework(http://loopj.com/jquery-tokeninput/)

(function ($) {
// Default settings
var DEFAULT_SETTINGS = {
    // Search settings
    method: "POST",
    contentType: "json",
    queryParam: "interestVal",
    searchDelay: 300,
    minChars: 1,
    propertyToSearch: "name",
    jsonContainer: null,

    // Display settings
    hintText: "Type in a search term",
    noResultsText: "No results",
    searchingText: "Searching...",
    deleteText: "&times;",
    animateDropdown: true,

    // Tokenization settings
    tokenLimit: null,
    tokenDelimiter: ",",
    preventDuplicates: false,

    // Output settings
    tokenValue: "name", // orig "id"
}

Why is this array empty??

7
  • Yes. Which means it isn't getting anything from the query? Commented Apr 13, 2012 at 1:12
  • Is something in PDO malformed? Commented Apr 13, 2012 at 1:14
  • whats does this show: print $interestValue; Commented Apr 13, 2012 at 1:20
  • I tried that too. The thing is though, when I type "running" into the input it calls the JS above and calls the php file. I dont see how it would output this value Commented Apr 13, 2012 at 1:31
  • I'm trying to set this up: loopj.com/jquery-tokeninput . I wonder if this is the reason "Your script must accept a GET parameter named q which will contain the term to search for" ?? Commented Apr 13, 2012 at 1:34

1 Answer 1

1

Don't use mysql_real_escape_string with prepared statements - binding parameters takes the user data out of the SQL code, so there are no SQL injection vulnerabilities. More importantly, mysql_real_escape_string only works AFTER you connect to the DB, not before, which is turning your value into false.

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

7 Comments

thanks for the reply. I remove mysql_real_escape_string. Now it just says "searching..." forever. I tried manually looking at the php script. Its still outputting json array var Array[] when I try to echo 'json array var '.$jsonArray; right after while{}
Then you should either set up xdebug, or start echoing values in your PHP. Also make sure to check your error logs.
You can't echo an array. Try print_r($jsonArray);
Yeah. you can see the outputting I tried above. It printed out empty values for both variables. I also checked the catch{} block for this file and its blank
Good catch @Ryan - it still is a blank array() though
|

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.