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.
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.
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);
Related
I have a web application where I actually use JQuery and Ajax in order to send a query to my database.
My script is something as simple as this:
$(document).ready(function(){
var datastr = id; // get the value inserted in text
var ajaxurl = 'run.php', // script to run
data = {datastr:datastr}; // data to pass
$.post(ajaxurl, data, function (response){...//do something
Making some improvements in my script it crossed my mind this: do I really need to make an Ajax request to send this query to the database or I can just use something as simple as the PHP function for PostgreSQL to do that?
Ofcource I know that Ajax is all about asynchronous communication but I want to know what would be the best practice in a situation like this. Is using Ajax in this case an overkill?
do I really need to make an Ajax request to send this query to the
database or I can just use something as simple as the PHP function for
PostgreSQL to do that
Well actually it does not have the same goal. As you may know, Ajax is client-side meanwhile PHP is server-side. If you want to send your request without refreshing your webpage, then you would need asynchroneous request like Ajax does. Else, PHP fit your need.
I'm using AJAX to insert data in MySQL database. During the AJAX request, there is a PHP function that loops inside a JSON array in order to get data and to insert it inside the DB. Everything works fine.
But, I would like to know if there is a way to pass, during the AJAX request a PHP var to jQuery in order to append it in HTML or to retrieve the data with console.log. I can get these info on AJAX complete but is it possible to get info during AJAX request?
I think you can just echo the php var on the page?
E.g. echo "<label>".$phpVarToAppend."</label>";
Nope. HTTP is stateless. You make a request and you get a result.
You must use different techniques to check the request's processing advancements from server.
Given that you store a record of the progression during the processing somewhere (db, cache or whatever), the simpler trick is using another AJAX call to a simple function that returns the last processing record.
This is a traditional polling mechanism.
A more advanced solution could be using a different connection upgraded to websockets. This will be a true realtime channel.
On top of these there's a world of possibilities. It only depends on what you need to manage with your POST request and how long does it take the processing.
For big payloads it's usually better to return immediately and start processing in a different task. (and thus a pooling mechanism to check progression)
I have three identical drop down lists that I'm populating using MySQL data.
If one of them changes index (onchange is fired), I want the other two to re-populate, so that they exclude the item that has been chosen with the other drop down list.
In ASP.NET, I would simply have called a function (passing through the chosen items' value) in the code behind to re-populate the other two.
I've been poking around for a while now and I just can't get it done with PHP. Does PHP allow function calling from this event? I've also tried using AJAX, but nothing.
You can't (directly) call server side functions from the client side.
You can make an HTTP request to a URL that runs a server side script that calls the function.
ASP.NET has (I believe, I've never used it) some stuff that will set up a URL and generate some JavaScript that will call that URL for you.
There is nothing in the PHP core to do that.
A very rough, quick and dirty outline (that makes no attempt to be RESTful) of how you might go about doing something like that would be:
callFunction.php:
<?php
include("myFunctions.php");
if ($_GET['call'] == "functionA") {
functionA();
}
?>
Then your JS would make an ajax request to callFunction.php?call=functionA.
OnChange() is a js function , in order to use it to call a php function you'll have to use AJAX . Using ajax you'll be able to get the php's code returned value , display it in the page , or doing whatever manipulation you'd like.
Good luck
ASP.NET would have been doing a Postback when the dropdown was changed, although you'd not have had to write it explicitly.
If you want to do a round trip to the server in PHP, you'll have to implememnt the same logic (e.g. post the form with JavaScript in the onchange event).
This is a good situation for using AJAX as well, but you haven't provided any details of what you tried or what went wrong so it's hard to help further.
I have the following scenario:
At onClick() event 2 javascript functions are called, each calling a AJAX POST request to a different php script. In these 2 php scripts data from 2 different databases are collected in 2 class objects. At the end of each script I'm calling a php function which checks if both php scripts ended successfully (checks a flag stored in $_SESSION). When the condition is met, I'm supposed to have 2 class objects populated with data from the 2 databases, and display them both side-by-side. At least that's what I'm trying to accomplish.
My problem is the data displayed is only for the 'last-script-to-finish'.
If you can suggest alternatives methods of how I could accomplish this, or see flaws in my logic please shout as I'm not a php or web developer at base.
TA!
you can do the following using jQuery:
$.when($.ajax("/page1.php"), $.ajax("/page2.php")).done(function(a1, a2){
var page1Response = a1[2].responseText;
var page2Response = a2[2].responseText;
});
So you know both Ajax requests are ready, then you don't have to solve this serverside since your calls are clientside. More information about jQuery.when.
It gives great effect when you stream info in real time using ajax. for this example a SEO / webgrader tool called www.teqpad.com gives a nice interface and real time information on each queries into its database. They use PHP/ajax for the purpose.
My questions are:
how do they do it?
What are the methods or steps to take care of?
How php need to be written in to show the same.
I am not asking code but the method or proceedure to write the same.
Thanks in advance
Use the javascript setTimeout() function to invoke GET requests to the server.
Server returns data from your database.
HTML is updated.
You do an AJAX call every few seconds with javascript and refresh the HTML of the page with the response of the call.
You could use JQuery to do the AJAX calls:
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
The call can be done to a PHP script. Depending the kind of information you want to get is the kind of operations you do on PHP.
Here is a simple example: http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp