Need "Toggle Switch" Advice


#1

I THINK there’s a very simple solution for my questions; I just haven’t learned how to do it yet. Let me break it into three steps:

  1. Consider this link to a JavaScript file in a web page’s head section:

The key code is <?php echo $unit ?>

  1. Put the entire head section in a PHP include page.

  2. On the target page, substitute a value for $unit and include the head section with the JavaScript link…

<?php $seg = '../../../'; $unit = 'length'; include ($seg."a1/inc/head.php") ?>

Another example…

<?php $seg = '../../../'; $unit = 'weight'; include ($seg."a1/inc/head.php") ?>

It works just fine; I have four identical pages with included PHP head sections that use the $unit variable to fetch JavaScript four different JavaScript conversion files that focus on length, area, weight and volume.

But I want to convert them to just ONE page that works with all four units. I think I need to simply make links that somehow toggle the $unit value between length, area, weight and volume.

Tentatively, I’d like it to look like this…

Choose a Unit:

Length | Area | Weight | Volume

(Length, Area, Weight and Volume would be linked.)

But I don’t know how to do it. I’m not even sure if I want to make PHP or JavaScript toggle switches. I’ve read many warnings that JavaScript functions are rendered useless if visitors use browsers with JavaScript disabled. On the other hand, many people claim JavaScript is superior to PHP because the latter requires the page to be refreshed. On the other hand, I’m not sure if refreshing the page is really a big deal in this case; it’s a very small page.

At any rate, can someone tell me how to make toggle switches - or some function - that will get the job done?

Thanks.


#2

I’ll ask the obvious question. Why don’t you have all the formulas in the same file? If I remember my geometry correctly, formulas for width, length, weight, and volume are pretty simple. Putting them all in one file will allow your clients to load the javascript source once, then it will be cached, instead of the overhead of downloading four files.

On the other hand, if you’re insistent on having four files, I would probably make each $unit a querystring and pass it back to the calling file. Make sure to validate the input, then you’ve got $unit set from the calling page.