views:

5686

answers:

3

I know how to do this with pure PHP but I need to do this without reloading the page. Is there anyway with jQuery to effectively pull back some database results (based on what a user has input in the first text field on a form) then populate some of the remaining fields with data pulled back from a db query?

Essentially I would like to see the user move away from the text field (either by tabbing out or by click in the next field) and boom, the query is submitted using the value entered in that field and the subsequent fields are then populated w/o a page reload.

I am familiar with the basics of jQuery but I haven't used it to do anything like this in which I am pulling data back from the server and trying to populate it client side.

Any suggestions / examples on how to best get started with this would be very much appreciated. Thanks.

  • Nicholas
+3  A: 

If you need to hit the database, you need to hit the web server again (for the most part).

What you can do is use AJAX, which makes a request to another script on your site to retrieve data, gets the data, and then updates the input fields you want.

AJAX calls can be made in jquery with the $.ajax() function call, so this will happen

User's browser enters input that fires a trigger that makes an AJAX call

$('input .callAjax').bind('change', function() { 
  $.ajax({ url: 'script/ajax', 
           type: json
           data: $foo,  
           success: function(data) {
             $('input .targetAjax').val(data.newValue);
           });
  );

Now you will need to point that AJAX call at script (sounds like you're working PHP) that will do the query you want and send back data.

You will probably want to use the JSON object call so you can pass back a javascript object, that will be easier to use than return XML etc.

The php function json_encode($phpobj); will be useful.

jskulski
Okay so for the url: property I pass it my php script but what about the data: property? What does $foo represent?
Nicholas Kreidberg
Look athttp://docs.jquery.com/Ajax/jQuery.ajax#optionsdata: $foo in basically the $_POST or $_GET string you might need to send to your script for context.so if you script is script.php?id=5 name: 'toasty' }
jskulski
Does this work: "data: id: $('#sid').val()," ? or should I set a variable (i.e. $foo) to the value of $('#sid').val() ?
Nicholas Kreidberg
I'm not solid on js syntax but i think it would have to bedata: { id: $('#sid').val() }because it is expecting an array
jskulski
+5  A: 

Assuming this example HTML:

<input type="text" name="email" id="email" />
<input type="text" name="first_name" id="first_name" />
<input type="text" name="last_name" id="last_name" />

You could have this javascript:

$("#email").bind("change", function(e){
  $.getJSON("http://yourwebsite.com/lokup.php?email=" + $("#email").val(),
        function(data){
          $.each(data, function(i,item){
            if (item.field == "first_name") {
              $("#first_name").val(item.value);
            } else if (item.field == "last_name") {
              $("#last_name").val(item.value);
            }
          });
        });
});

Then just you have a PHP script (in this case lookup.php) that takes an email in the query string and returns a JSON formatted array back with the values you want to access. This is the part that actually hits the database to look up the values:

<?php
//look up the record based on email and get the firstname and lastname
...

//build the JSON array for return
$json = array(array('field' => 'first_name', 
                    'value' => $firstName), 
              array('field' => 'last_name', 
                    'value' => $last_name));
echo json_encode($json );
?>

You'll want to do other things like sanitize the email input, etc, but should get you going in the right direction.

Parrots
I can't seem to get this to work...Error:G is undefinedinit()()jquery-1....1.min.js (line 12)(?)()()test.html (line 15)I()jquery-1....1.min.js (line 19)F()()jquery-1....1.min.js (line 19)[Break on this error] (function(){var l=this,g,y=l.jQuery,p=l.....each(function(){o.dequeue(this,E)})}});
Nicholas Kreidberg
Is there any effective way to show you my complete source? Can't do it here with the 300 character limit...
Nicholas Kreidberg
Have a server you can post it on?
Parrots
Not that is public... Can I email to the form on your blog?
Nicholas Kreidberg
Sure, that'll work fine
Parrots
Thank you very much sir, I just sent it.
Nicholas Kreidberg
Did it come through okay via the web form?
Nicholas Kreidberg
Yeah, although I'm away from my computer till later tonight.
Parrots
pastebin.com is for pasting codesnippets.
Ikke
A: 

Nicholas Kreidberg, thanks for asking this because thats the exact problem that am facing at the moment. I want to do it with pure php and seeing that you have already done it, could I request you to show me how? Please???