динамически настраивать выбор селектора списка веб-страниц с помощью ответа ajax json - PullRequest
1 голос
/ 12 мая 2011

Я пытаюсь разработать приложение, которое получает ответ из базы данных MySQL, используя ajax post и update в селекторе списка, но список отображается пустым, может кто-нибудь помочь мне в этом, пожалуйста .....

код для .js:

SecondAssistant.prototype.setup = function() {
this.selectorChanged = this.selectorChanged.bindEventListener(this);
Mojo.Event.listen(this.controller.get('firstselector'), Mojo.Event.propertyChange,     this.selectorChanged);

this.names = [];

try {
    new Ajax.Request('http://localhost/projects/testingasdf.php', {
        method: 'post',
        parameters: {
        'recs': getallrecords,
        'q': q
        },
        evalJSON: 'true',
        onSuccess: function(response){
            var json = response.responseJSON;
            var count = json.count - 1;

            for(i=0; i<count; i++){
                this.names.push({
                    label: json[i].name,
                    value: '0'
                });
            }
                       this.controller.modelChanged(this.model);
        }.bind(this),
        onFailure: function(){
            Mojo.Controller.errorDialog('Failed to get ajax response');

        }

    });

}
catch (e){
    Mojo.Controller.errorDialog(e);
}

this.controller.setupWidget("firstselector",
          this.attributes = {
              label: $L('Name'),
              modelProperty: 'currentName'
          },
          this.model = {
              choices: this.names
          }
        ); 

};

код для php:

<?php
header('Content-type: application/json');  // this is the magic that sets responseJSON


$conn = mysql_connect('localhost', 'root', '')// creating a connection



mysql_select_db("test", $conn) or die('could not select the database');//selecting database from connected database connection

switch($_POST['recs'])
{
    case'getallRecords':{
        $q = $_POST['q'];

        //performing sql operations
        $query = sprintf("SELECT * FROM user WHERE name= $q");
        $result = mysql_query($query) or die('Query failed:' .mysql_error());
        $all_recs = array();
        while ($line = mysql_fetch_array($result,MYSQL_ASSOC)) {
        $all_recs[] = $line;
        }
        break;
        }   
        }

echo json_encode($all_recs);

// Free resultset
mysql_free_result($result);


// closing connection
mysql_close($conn);
?>

1 Ответ

1 голос
/ 12 мая 2011

Я бы переместил код обновления модели из метода SecondAssistant.prototype.setup и запустил его где-нибудь в SecondAssistant.prototoype.activate.

Также вызову modelChanged

this.controller.modelChanged(this.model);

Тамявляется опечаткой в ​​bindEventListener - должно быть bindAsEventListener, а возвращаемое значение привязки должно быть другим объектом:

this.selectorChangedBind = this.selectorChanged.bindAsEventListener(this);
...