I am trying to make a plugin that people can place on their site to generate a form. I dont want to use an iframe to display the form but use javascript to generate it.
The problem is how do i connect the javascript and php together. My site is programmed in PHP.
I am trying to make a plugin that people can place on their site to generate a form. I dont want to use an iframe to display the form but use javascript to generate it.
The problem is how do i connect the javascript and php together. My site is programmed in PHP.
Share Improve this question edited Dec 19, 2010 at 16:42 Jakub 20.5k8 gold badges66 silver badges93 bronze badges asked Dec 19, 2010 at 16:18 sammvillesammville 3832 gold badges4 silver badges14 bronze badges 2- Do you mean you want to use Javascript to generate a form on any domain, using data generated by PHP from you own domain? – Harmen Commented Dec 19, 2010 at 16:25
- yes!! that is what i want to do. Php brings the data from the database and supplies it to javascript whcih displays it. – sammville Commented Dec 19, 2010 at 16:30
3 Answers
Reset to default 1Your getting a liite mixed up, I think.
PHP runs on your server. This is the place where you fetch data from the database and create some form of html-response.
Javascript runs in the browser. It can't directly talk to your database.
iframe is a special html-element: Therfore it is passive and can't do anything like creating a form.
You have two ways:
- Create a PHP script which handles everything through plain HTTP-Requests. This is the "old school" way and requires a lot of page-reloading.
- Write most of the logic in javascript and let it municate to PHP/your database through AJAX. In this case. Have a look at jQuery which makes AJAX-requests (and a lot of other things) very easy.
One issue you will be faced with is 'Cross site Scripting' with Javascript / AJAX. You can read up on it a bit here:
http://snook.ca/archives/javascript/cross_domain_aj
Also, thinking your process through, you will need sufficient javascript code to create a 'widget' on any place, and have a way to municate BACK to your server (keep in mind PHP only runs local on your machine, so it cannot be used remotely in your javascript).
You will probably need to build a JSON API (google / stack search this if needed). And enable munication from your JAVASCRIPT to the API (don't think of it as to PHP, even tho php will be your API server side language).
Here is an example of a PHP JSON API (on youtube too):
http://www.youtube./watch?v=F5pXxS0y4bg
If you put PHP into JavaScript and someone implements this, PHP will pile on their server. So you just can't. You just need to put the form in your plugin.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744955126a4603147.html
评论列表(0条)