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.
Related
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);
I have a PHP script I use to register users but during that process I'm trying to call a Google GEOCoding javascript method to return the client lat/lng and record it to a DB before continuing with my PHP registration process.
Can this be done or is there a better way?
My current code looks something like this:
private function process() {
$this->validate();
$this->register();
// execute javascript here to record lat/lng coordinates to the DB.
$this->goHome();
}
You don't need javascript. The Google Geocoding API is a web service. In PHP, just do a file_get_contents() and issue the request using the proper url format as described here. You'll then need to run the result through PHP's json_decode().
You can't just run JavaScript in a PHP script. PHP is executed on the server, and JavaScript is executed in the client's browser. You should modify your registration form so that the JavaScript is called before the form is submitted, and you should pass the user's lat/lon as extra data for registration. Then, when your PHP registration runs, you'll have access to this data.
A better idea would be to find a PHP library to do geocoding.
See Johnathan M's answer.
Javascript is a clientsided script language
Php is serversided
This makes it impossible the way you imagion to do it.
Take a look into GeoIP
http://php.net/manual/en/book.geoip.php
You generally handle only one request in script and what you asking about would require at least two: in first user would ask for some response from your server and you'll be able to send your JS to him and in second request he'd submit results of running that JS.
You can save "state of waiting for response" in some way and process geo response when it comes.
I think the easiest way to solve your issue would be to use 2 AJAX calls and a function:
- the first calls a PHP script to do your validation/ registration
- when this completes run a clientside JS script to do the geocoding
- have another AJAX call to update the DB with the result of geocoding
You should be able to solve your problem this way. If you use a library like jQuery its easy to do the AJAX calls and you can even make use of deferred objects, which I think could help you chain your calls http://api.jquery.com/category/deferred-object/
Do the GeoIP lookup in PHP using a web API instead on the server side.
For example:
http://www.maxmind.com/en/web_services
They have a PHP example here (I wouldn't call the webservice that way, but it works):
http://dev.maxmind.com/geoip/web-services#PHP-11
private function process() {
$this->validate();
// Do a call to the webservice here with PHP, update user data with the response.
$this->register();
$this->goHome();
}
There are a couple of ways to go about it.
The best appoach would be to make the geocaching call within your PHP, not using javascript all. PHP has several ways of making HTTP requests from code, that will return results you can use in your script. I personally favor cURL: http://php.net/manual/en/book.curl.php. See also Jonathan M's earlier reply, same idea but using file_get_contents().
Another (far hackier) approach would be to make the javascript call to the Google Geocoding API synchronous (async=false in jquery.ajax()), and have it execute your request to your registration php page through a callback on the ajax call. (The "success" property on the ajax.) This means that your registration page will not be called until the geocoding lookup is completed. It's a valid question whether you want to make success/failure of your registration dependent upon completion of the ajax request, but if you do, this would work. Check here for info on synchronous ajax requests in jquery: http://api.jquery.com/jQuery.ajax/. This really isn't the best way to go about it though. The geocoding lookup really is part of the registration, and the last thing you want to do is make your php registration dependent on unreliable frontend behaviors.
To be specific, I want to make code like Yahoo's Registration (Yahoo ID - Check Availability) checking the availability. The problem here is I need to use PHP to connect, get, and search/verify if the entered value in the textbox is unique.
I tried passing a JavaScript value to a PHP variable, but based on my research, doing this isn't possible, so I need another possible solution to get the value of the textbox and set it to a PHP variable.
Thank you in advance.
The only way to get data from JavaScript to PHP (in a typical environment) is to deliver it over HTTP. Doing this without leaving the page is known as Ajax, and usually done using XMLHttpRequest.
There are no shortage of tutorials describing how to do this, nor any shortage of libraries that will help (plus, of course, all the large libraries such as YUI and jQuery).
You can use Ajax for manipulating this situation.. Have a look at following link.
http://www.digimantra.com/technology/javascript/check-username-availability-validation-ajaxphp/
You need to use AJAX for that.
AJAX is used to send and receive data from the server. So, on the server side, you can create a URL that checks whether a username (can be passed using get/post) is available or not & accordingly send a response. At the client side, depending on the response you receive from the server, you can alert the user.
I am thinking of developing a website in PHP and I was thinking of using AJAX in order to send data to, and retrieve data from a server asynchronously (in the background) without interfering with the display and behavior of the existing page.
So my question: would it be better to use AJAX to GET or POST to php pages or any other options are possible?
There are not that many options when it comes to submiting data, either you use AJAX or use the normal http message like GET or POST. If you would like to determin witch is better i would say: it depends.
Ajax seems to me like a great way of making more dynamic a part of your site, but i dont think its always practical to use in all your site. Ajax is generally used when you need to show some context change on one page, like posting a commner, faving a question, or things like that. Another great thing about it is not to bound user to a form (you can save info with a link like when you vote a question here)
Using GET or POST its usefull to let the user now here's moving along a process or when things that happen after or before are different, or context change a lot when submitting.
Rule of thumb (regardless of AJAX).
If you're going to send large amounts of data, or sensitive data... POST.
Otherwise, GET works just as fine.
I recommend use JQuery.
with $.ajax of Jquery, you can use it with multiple options depending that you need.
So you can work with POST, GET, receive data like text, json...
Here you can get more info:
http://api.jquery.com/jQuery.ajax/
I always use $_POST. I wish I could give a reason why I think it's better, but I can't. I guess it's because I've always preferred sending data via POST rather than GET so the user doesn't see it, and it just carried over to my ajax.
I would use $_POST just for the fact that it can store more data.
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