Page 1 of 1

calling PHP functions from JS?

Posted: Thu May 30, 2002 4:16 am
by izvit
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 :)

Thanks,

-ViT

Posted: Thu May 30, 2002 4:39 am
by Datamike
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.

Posted: Thu May 30, 2002 5:01 am
by izvit
Thanks for the reply. :)
I realized that is the problem I ran into, and I am wondering if there is some way to accomplish what I am trying to do.

The only way I could think of is to reload the page onChange of the dropdown menu and loading the necessary data into the textbox.

If anyone has any better solutions, help is very appreciated :)

Thanks

-ViT

Posted: Thu May 30, 2002 9:40 am
by Johnm
If all that you are trying to do is change the options in a select box try these js functions with the onChange event handler.

Code: Select all

// Sets options in an select box
function loadOptions(oElement,szNameVal)
{
    var aNV = szNameVal.split(",");
    for (x=0; x<aNV.length; x++)
    &#123;
        aItem = aNV&#1111;x].split('=');
	oElement.options&#1111;x]=new Option(aItem&#1111;0], aItem&#1111;1]);
    &#125;
&#125;
Call it like this:

Code: Select all

if (some_condition)
{
    loadOptions(form.fieldName,"option1=option1, option2=option2");
}
adding as many options as you like.
Hope that helps,

Direwolf

Posted: Thu May 30, 2002 10:39 am
by izvit
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.


-ViT

Posted: Thu May 30, 2002 11:03 am
by Johnm
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.

Direwolf23

Posted: Thu May 30, 2002 11:39 am
by izvit
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.

Thanks anyway,

-ViT

Posted: Thu May 30, 2002 12:21 pm
by mikeq
But would you really be giving the user a drop list with 1000's of items?

Mike

Posted: Thu May 30, 2002 12:46 pm
by izvit
yeah... definitely a design flaw :roll:. 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 :D
Thanks

-ViT