开发者

Jquery toolbar help: accessing external php script

Hi i'm currently working on a toolbar and have run into a problem. I have two files, the toolbar and a PHP script. The PHP script simply pulls an array of titles out of a database. What I am trying to do is get the toolbar to call this script and return this array, and then use this array to create a drop down menu with the titles as the options.

I'm assuming I need to use Ajax in some form but have no idea how to achieve this.

Many Thanks.

UPDATE: I am having problems getting .getJSON work cross browser because of the same site policy. The problem is the javascript file making the call produces a toolbar which is appended to a remote site, this toolbar is supposed to call a php script back on my own server to get data f开发者_Go百科rom a db.

How can I get around this issue?


Yes. If you want to have PHP and Javascript working together the answer is: JSON. You will have to JSON encode your array in php. Use Javascript Ajax to call the PHP file and then parseJSON the response. Then you have your array in use in Javascript. After that you can render it in Javascript and just attach the result to the element you want.

There are plenty of examples in jQuery documents. If you are unfamiliar with ajax, read the documentation: http://api.jquery.com/jQuery.ajax/ In the end there are simple examples. You can also use .getJSON.

jQuery.getJSON( url, [data,] [success(data, textStatus, jqXHR)] )    
- url A string containing the URL to which the request is sent.    
- data A map or string that is sent to the server with the request.    
- success(data, textStatus, jqXHR) A callback function that is executed if the request succeeds.

Basically:

$.getJSON("url of the php file",
          data to send to the php file if any,
          function(data) {
            console.log(data);
          }
});

Inside that function where is the console.log you can do anything you want with your data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜