views:

643

answers:

2

I'm trying to populate a form with jquery's populate plugin, but using $.ajax

The idea is to retrieve data from my database according to the id in the links (ex of link: get_result_edit.php?id=34), reformulate it to json, return it to my page and fill up the form up with the populate plugin. But somehow i cannot get it to work. Any ideas:

here's the code:

 $('a').click(function(){
     $('#updatediv').hide('slow');
     $.ajax({
   type: "GET",
   url: "get_result_edit.php",
   success: function(data)
      {
                                   var $response=$(data);
                                   $('#form1').populate($response);           
                               }
            });
     $('#updatediv').fadeIn('slow');
     return false;

whilst the php file states as follow:

<?php
$conn = new mysqli('localhost', 'XXXX', 'XXXXX', 'XXXXX');
@$query = 'Select * FROM news WHERE id ="'.$_GET['id'].'"';

$stmt = $conn->query($query) or die ($mysql->error());
if ($stmt) 
  { 

     $results = $stmt->fetch_object(); // get database data
    $json = json_encode($results); // convert to JSON format
      echo $json;

      }


?>

Now first thing is that the mysql returns a null in this way: is there something wrong with he declaration of the sql statement in the $_GET part? Second is that even if i put a specific record to bring up, populate doesn't populate.

Update:

I changed the populate library with the one called "PHP jQuery helper functions" and the difference is that finally it says something. finally i get an error saying NO SUCH ELEMENT AS i wen into the library to have a look and up comes the following function

function populateFormElement(form, name, value)
{
    // check that the named element exists in the form
    var name = name; // handle non-php naming
    var element = form[name];
    if(element == undefined)
    {
        debug('No such element as ' + name);
        return false;
    }

    // debug options       
    if(options.debug)
    {
        _populate.elements.push(element);
    }
}

Now looking at it one can see that it should print out also the name, but its not printing it out. so i'm guessing that retrieving the name form the json is not working correctly.

Link is at http://www.ocdmonline.org/michael/edit%5Fnews.php with username: Testing and pass:test123 Any ideas?

A: 

First you must set the dataType option for the .ajax call to json:

$.ajax({dataType: 'json', ...

and then in your success function the "data" parameter will already be a object so you just use it, no need to do anything with it (I don't know why you are converting it into a jQuery object in your code).

edit:

$( 'a' ).click ( function () {
 $( '#updatediv' ).hide ( 'slow' );

 $.ajax ( {
  type: "GET",
  url: "get_result_edit.php",
  success: function ( data ) {
   $( '#form1' ).populate ( data );
  },
  dataType: 'json'
 } );

 $( '#updatediv' ).fadeIn ( 'slow' );
 return false;
}

also consider using $.getJSON instead of $.ajax so you don't have to bother with the dataType

Jan Hančič
Are you getting any errors? Is the return data from your PHP file in the correct format? Provide more info.
Jan Hančič
well first of i think there's something wrong with the sql query since its returning null, but if i put i change it to get the last entry in the db it gets a result {"subject":"New Topic with no pic","date":"14\/11\/2009","image":"","content":"contentgoes here","id":"38"} using firebug to see the response. The only thing is that its not populating the form. I've adjusted a couple of thing and i believ its not reading the json correctly, since its not detecting the field name, and so i cannot compare it with the form if it exists or not
Azriel_
Key names in your JSON must match the names of your form fields for this to work. Since you are not getting any errors I believe this is your problem.As for the SQL, you must pass the ID in your $.ajax call. If you do that do you get any SQL errors?
Jan Hančič
how do i pass the id in the $.ajax call?the id's in the form fields are the same. the only thing i'm suspecting is that looking at the examples the keynames in the JSON there are without quotes, whilst in mine they are retrived with double qoutes ex. "subject":"New Topic with no pic". any idea of a work around?
Azriel_
ok i manage to pass with the ajax using the (this).attr("href") as url now i need a workaorund on the JSON
Azriel_
Quotes don't matter. Are your name (not ID) attributes of your fields the same as the fields in your JSON object?
Jan Hančič
yep they are the same <input type="text" name="subject" id="subject" class="subject" />
Azriel_
A: 

Try imPagePopulate (another jquery plugin). It may be easier to use:

http://grasshopperpebbles.com/ajax/jquery-plugin-impagepopulate/

Les Green