0

I have a HTML input that a user will type into. After every key a JQUERY event will grab the text written so far and make an AJAX call that will be picked up by PHP.

I have done this before and its easy to have a PHP script that is called that will then deal with the data passed - eg: db calls and return what is required.

Question - Is it possible to do this using a PHP object? (I'm writting OOP PHP for the first time). Is there some way to combine an AJAX call with a PHP Object or should the PHP end just be a standard function?

thank you

* More info: this function is searching for a location against a database with 6million suburbs/postcodes. Each key a user types a jquery event it trigger that creates an ajax call with the letters typed so far. PHP is called and a SQL query run to return possible locations.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Adam
  • 19,932
  • 36
  • 124
  • 207
  • I do not really understand your question/requirement, but I think what you need is JSON. – KBN Feb 05 '13 at 06:05

2 Answers2

2

You can combine a PHP object with an AJAX call, but you'd have to explain what you're trying to do in greater detail for me to determine whether or not it would help you.

It sounds like you just want an AJAX call that is invoked after say, 500ms of a user not giving input to the text box or whatever. That seems like a standard PHP update function.

Based on what you've stated so far, I would suggest you just go with the AJAX; have it proc off an onKeyUp, onKeyPress, or whatever you want, and then run some PHP.

Recall that you can create a timer in many fashions; just search around here. You probably don't want to use AJAX for each key stroke.

OnKeyUp JavaScript Time Delay?

Hope that helps.

Community
  • 1
  • 1
Eric Hotinger
  • 8,957
  • 5
  • 36
  • 43
  • cool - how can I get AJAX to interact with a PHP object? I'm trying to understand this connection. From my understanding AJAX/JQUERY calls a PHP script and this non-object based. How could this instantiate an object? – Adam Feb 05 '13 at 05:27
  • Let's say you call a PHP function `function Ajax_GenerateFruit($query){}` and `$query` is an array of information where `$query['type']` tells us which type of Fruit object to make. Inside of this function you could say something like: `$fruit = CreateNewObject($query['type']);` Then maybe you say `$response = $fruit->getResponse();` And you then `return $response` If you have multiple kinds of fruit, you could have multiple kinds of responses! In this case, a new instantiation of a Fruit would be made after each AJAX call. Hope this helps. – Eric Hotinger Feb 05 '13 at 05:32
1

Use javascript (read jQuery) to submit a post message to your server when the client has paused input for a period of time.

If you are trying to submit an entire form of data, there is a useful function in jquery that will serialize it for you:

var formData = $("#form").serializeObject();
$.post('phpPageURL.php', formData, function(data){
       //analyze success or failure
    });

Then, write some php which will convert that post data into a model (class) which is how the rest of your application interacts with that data. Then, depending on what you want to do with that data call the necessary controller, passing it that object.

Something like:

$foo = new Foo();
$foo->name = $_POST['formField_name'];

ManipulateData::doawesomeness($foo);