PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I was wondering if it is possible to call a PHP function from Javascript?
What I am trying to do is to have a drop down menu(combo box), and based on the selection, retrieve the necessary information on the item from a MySQL database and place it in a textbox.
If anyone has any suggestions, I would greatly appreciate them
No, it is not possible to call PHP function from Javascripts. The reason for this is simple the fact that PHP is executed on server-side and Javascript is executed on client-side. What happens is that when you type the address to your addressbar and hit "enter" your browser will find tha server that is marked to hold that domain. Your browser then asks for the server for a certain file and if that file is found and contains server-side code (like PHP or Perl) the server executes that code and then returns the file to you. After that your browser goes through that file and executes, if any, the Javascript code.
See, only the server can execute the PHP code, which is no longer in the file that is returned to your browser. This makes it impossible for the Javascript to execute any PHP functions.
// Sets options in an select box
function loadOptions(oElement,szNameVal)
{
var aNV = szNameVal.split(",");
for (x=0; x<aNV.length; x++)
{
aItem = aNVїx].split('=');
oElement.optionsїx]=new Option(aItemї0], aItemї1]);
}
}
well... that would work if I had the access to the data, but the problem is accessing this data, which is in the database, in real time without reloading the webpage.
Basically, whenever the user selects different items in the drop down menu, I need a way to connect to a MySQL database, retrieve the data on that selected item and place it in a textbox.
Hmmm,
well, is there so much data that you couldn't use hidden inputs and have them on hand as form elements?
In one situation I query the database for the possible options, sending the results to two separate arrays (one for var name, one for value... you could use one I think) and then have a function to create them as hidden elements so that I have access to them. This will not be a viable solution it you have a extremely large amount of options due to it's inherent lack of efficiency but I have working examples if you are interested in exploring this rout.
right... exaclty, to load the whole database into an array would be a some what of an issue, since I got 1000s of database entries and growing. It is a very good idea, but I am not sure I can use it in this case.
yeah... definitely a design flaw . There shouldn't be more than 300 at a time, which would still be too much, but thanks again for the suggestion, Direwolf, it will definitely help me out a lot
Thanks