0

I have been trying to extract data from a database and fill in a drop down list generated by JavaScript. However the PHP code doesn't seem to work at all. Here is the snippet code I am working with:

<script type="text/javascript">
    $(document).ready( function () {$('#mainTable').dataTable({}).makeEditable({
   "aoColumns": [
{
    indicator: 'Saving Items...',
    tooltip: 'Click to select Items',
    loadtext: 'loading...',
    type: 'select',
    onblur: 'submit',
    <?php
        $item_set = get_all_items();
    while($item = mysql_fetch_array($item_set)){
      ?>
       data: "{'0':'Please select...', '1':'A'+' <? echo $item["item_desc"] ?> '}",
    <?php
        }
    ?>
      sUpdateURL: function(value, settings){
   alert("Custom function for posting       results");
   return value;
 }
 },                 
 {
 }
 ]
 });
} );
</script>

I have tried the code in a simple HTML form and it works just fine. Btw I am using jQuery editable datatable http://code.google.com/p/jquery-datatables-editable/wiki/Overview

I just want to populate the select menu with data from the database. Any help is appreciated.

Here is the generated page: I the first column of the table, the drop down list must also have item description from DB.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
altsyset
  • 339
  • 2
  • 20
  • 1
    possible duplicate of http://stackoverflow.com/questions/2194563/calling-javascript-within-php-block-and-vice-versa – Carrie Kendall Mar 27 '12 at 16:10
  • 1
    possible duplicate of http://stackoverflow.com/questions/1968296/how-to-i-send-data-from-javascript-to-php-and-vice-versa – Carrie Kendall Mar 27 '12 at 16:11
  • This topic is heavily covered on SO. try searching next time. – Carrie Kendall Mar 27 '12 at 16:12
  • 2
    It's not really a duplicate of this question. @altsyset can you please show us the HTML code that is produced by your script? – Till Helge Mar 27 '12 at 16:13
  • possible duplicate of [Pass a PHP string to a Javascript variable (including escaping newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newlines) – outis Mar 28 '12 at 04:50

2 Answers2

1

View your HTML source. You'll see that you have multiple data lines. Use json_encode to convert an array to Json instead of trying to string it together.

<?php
    $item_set = get_all_items();
    while($item = mysql_fetch_array($item_set)){
        $items[] = $item['item_desc'];
    }
?>

data: <?php echo json_encode($items); ?>,
AndrewR
  • 6,668
  • 1
  • 24
  • 38
  • Thanks for the help! But it didnt work out so I moved the pho code into another file and called it like this: data: "" – altsyset Mar 27 '12 at 19:23
0

It looks like you are looping in the wrong spot. Your loop will create the 'data' attribute several times. Maybe you want this:

onblur: 'submit',
data: "{'0':'Please select...', <?php 
$item_set = get_all_items();
$count = 1;
while($item = mysql_fetch_array($item_set)){
    echo "'".$count."':"
    echo "'".$item["item_desc"]."',";
}?> }",
sUpdateURL: function(value, settings){...
christopher_b
  • 958
  • 5
  • 13
  • Thanks for the help! But it didnt work out so I moved the pho code into another file and called it like this: data: "" – altsyset Mar 27 '12 at 19:24