Cannot populate form with ajax and populate jquery plugin

Posted by Azriel_ on Stack Overflow See other posts from Stack Overflow or by Azriel_
Published on 2009-12-07T12:52:01Z Indexed on 2010/04/11 15:03 UTC
Read the original article Hit count: 402

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about jquery-ajax

Related posts about jquery-plugins