2

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.

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? 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. Commented Dec 19, 2010 at 16:30

3 Answers 3

1

Your 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:

  1. Create a PHP script which handles everything through plain HTTP-Requests. This is the "old school" way and requires a lot of page-reloading.
  2. Write most of the logic in javascript and let it communicate 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.
Sign up to request clarification or add additional context in comments.

Comments

0

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 communicate 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 communication 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.com/watch?v=F5pXxS0y4bg

Comments

0

If you put PHP into JavaScript and someone implements this, PHP will compile on their server. So you just can't. You just need to put the form in your plugin.

1 Comment

this should be a comment, not an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.