Norse
Norse

Reputation: 5757

PHP JQuery/JS - Set PHP variable to Javascript variable?

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?

Upvotes: 0

Views: 335

Answers (3)

ebuoe
ebuoe

Reputation: 109

You could use AJAX to load it asynchronously.

Upvotes: 0

Ryank
Ryank

Reputation: 507

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?

Upvotes: 1

Bogdan
Bogdan

Reputation: 44526

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

Upvotes: 2

Related Questions