0

I have a huge <select> input that I call across many pages. My idea is to have this dropdown box as its own PHP file, and load it externally with JQuery. Is this even possible? Here's an example of what I'm trying to do:

if ($variable) {
   echo '<select>
         <option value="A">A</option>
         <option value="B">B</option>
         <option value="C">C</option>
         </select>';
}

If I had to make changes to the dropdown, it would be quite frustrating to go through each page that it exists on and edit this dropdown. Is there a way to load it with JQuery?

2
  • 1
    have you tried jQuery load method? Commented Jul 26, 2012 at 21:58
  • @Ramminson I don't understand how you would execute that into the PHP code Commented Jul 26, 2012 at 22:01

3 Answers 3

2

Why not just store the dropdown contents in a variable in a separate php file and include the file in any script you need to use it? Let's say dropdown.php looks like this:

$dropdown = '<select>
     <option value="A">A</option>
     <option value="B">B</option>
     <option value="C">C</option>
     </select>';

and then just do

include "dropdown.php";
echo $dropdown;

wherever needed

3
  • I had the same thought, except if the file just contains the html select menu I see no reason to store it in a variable. Might as well just be a plain text file, just read and print its contents. Commented Jul 26, 2012 at 22:05
  • Yes you can do that also. Just plain text file and include "dropdown.html" for example (no extra echo needed in that case). The idea was there is no need to jQuery or javascript. Commented Jul 26, 2012 at 22:07
  • So simple, how did I not think of this. Thanks. Commented Jul 26, 2012 at 22:09
1

Even though JQuery has a load feature for loading page fragments through AJAX I wouldn't recommend including this via a client side language as you cannot predict your end user and in-turn could be manipulated.

I would recommend including it via PHP or at most with JQuery / Javascript AJAX - have a look at this: http://api.jquery.com/jQuery.ajax/

More information about what you are looking to achieve with it would also help? e.g. any reason why you were thinking of using JQuery?

0

You could use AJAX to load it asynchronously.

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.