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?

share|improve this question

1  
have you tried jQuery load method? – undefined Jul 26 at 21:58
@Ramminson I don't understand how you would execute that into the PHP code – Norse Jul 26 at 22:01
feedback

3 Answers

up vote 2 down vote accepted

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

share|improve this answer
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. – travis Jul 26 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. – spider Jul 26 at 22:07
So simple, how did I not think of this. Thanks. – Norse Jul 26 at 22:09
feedback

You could use AJAX to load it asynchronously.

share|improve this answer
feedback

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?

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.