0

I have managed to get this code to work for an application -- http://twitter.github.io/typeahead.js/ but, the problem I have run into, is that I need to be able to re-load the data. I would rather not duplicate the code to do this, although it's an option, it seems kind of silly. I have inserted some PHP into my JavaScript to create an array for the dropdown list, and it works great. But if I stuff it into a function, and then try to call the function in the document open, it doesn't work (nothing appears to happen).

$(document).ready(function()
  {

     // call function when page loads(?)
     getAwards();

  }); // end document.ready ...

This is the PHP code, if I comment out the JavaScript function parts it runs and the typeahead code sees the value of the array. If I put it into the function, it doesn't execute despite being called above, and I have no data for the typeahead code ...

     function getAwards(
     {
        // build array for use with typeahead:
        <?php
           $sql_statement = "select title from awards order by title desc limit 1";
           $aw_result = mysqli_query( $connect, $sql_statement );
           $aw_row = mysqli_fetch_array( $aw_result );
           $last_award = $aw_row["title"];

           // need to rewind table:
           mysqli_data_seek( $aw_result, 0);

           // start from the top:
           $sql_statement = "select title from awards order by title";
           $aw_result = mysqli_query( $connect, $sql_statement );
           $count = 0;
           $out = 'awards = [';
           while ( $aw_row = mysqli_fetch_array( $aw_result ) )
           {
              // the quotes deal with forcing this to handle
              // branch names with apostrophes in them ...
              $count++;
              $out .= '"'. $aw_row["title"] . '"';
              if( $aw_row["title"] != $last_award )
              {
                 $out .= ',';
              }
           }
           $out .= ']';
           echo $out . "\n";
        ?>
     })

I need to be able to update the data, and reload the list while working on the form (I am working that out in my fuzzy brain, but anyway I'll get to that -- currently intend to click a button to update the list used by the typeahead, which is why I want a function ...)

Any suggestions are gratefully accepted. I am at a loss ...

Ken Mayer
  • 115
  • 15
  • Where are you defining this function that is being called? I would really suggest looking into doing an ajax call to a php function over what you have here though and not building a string that looks like an array... – nerdlyist Jan 31 '18 at 18:46
  • It's in the JavaScript at the bottom of the code, after everything else. At this point, while using the ajax call is fine and dandy, right now i just want this to work properly ... then I can fine-tune the code. – Ken Mayer Jan 31 '18 at 19:58
  • There are issue you are running into by calling it in a function. Do so it loses `document` as a context and thus cannot update it. You might spend more time trying to get it to work just to change it to a manner that makes sense. – nerdlyist Jan 31 '18 at 20:13
  • Well then, be helpful. Show me. My understanding of Ajax is very poor ... – Ken Mayer Jan 31 '18 at 21:01
  • https://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function – nerdlyist Feb 01 '18 at 14:11
  • I don't have the ability to close this question. There were enough issues with the code that I gave up and created my own routine. Not as cool looking, but it does the job. – Ken Mayer Feb 08 '18 at 21:45

0 Answers0