Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

Will Bontrager Blogs Website Techniques

WillMasterBlog > JavaScript

Dynamic Year List Dropdown

Form dropdown fields with year selections related to the current year need to be updated at the start of every year. That is, unless you have the dropdown set up to automatically stay current, which is what this article is about.

An example of selections related to the current year are credit card information forms, where you may want the current year and the next 6 years available for selection. Another example is a form to select the year for a calendar download, in which case the next year and some years thereafter should be selectable.

The first of the following examples lists the current year and next year. The second lists five year numbers, beginning with next year. The third lists the 8 previous years.





The above dropdown selection examples are created with this JavaScript. Following the code are instructions.

<select>
<option value="">-- Select Year --</option>
<script type="text/javascript">
var StartYear = +0; // use +0 to start with the current year; -1 for last year; +1 for next year; +3 for three years from now; or so forth.
var HowManyYears = 2; // How many years to list in the selection.
var today = new Date();
var year = today.getFullYear();
for(var i=0; i<HowManyYears; i++)
{
   var y = (year+StartYear)+i;
   document.write('<option value="' + y + '">' + y + '</option>');
}
</script>
</select>

The first 2 lines and the last line of the above code represent your selection form field:

  • (line 1) your select tag.
  • (line 2) the optional first selection for instructions.
  • (last line) the closing </select> tag.

Your select tag might have an id attribute and/or a name attribute. It might also have other attributes.

In between the code that represents your selection form field is the JavaScript that writes the options with the year numbers. The JavaScript has two places to edit:

  1. Update the +0 value in the var StartYear = +0; line.

    To start the dropdown list with the current year, specify +0 as the value.

    To start the list with last year, specify -1 as the value or to start the list with next year, specify +1.

    The digit you use represents the number of years. The "+" or "−" represents the next or past, respectively. As an example, +3 starts the dropdown list with the year number for 3 years in the future.

  2. Update the 2 value in the var HowManyYears = 2; line.

    The number that 2 is replaced with tells the JavaScript how many year numbers to populate the dropdown with. (Year numbers will be sequential, ascending.)

The JavaScript automatically lists the year numbers in the dropdown in relation to the current year. It gives you a year list.

(This content first appeared in Possibilities newsletter.)


WillMasterBlog > Content Protection

How To Prevent Web Page Printing

Generally, when a browser's menu is used to print a web page, the web page prints. Even if the site owner wouldn't want it to print.

Various browsers have various ways to cause a web page to print. The "File" menu item of browsers running on desktops/laptops often has a "Print..." selection available. Control-p or Command-p may be used with some browsers. For devices, you may find a print menu item under "share" or "page" options. Every browser I've observed with this in mind had some way to print the web page.

Perhaps the web page contains a temporary link to a download. Or the person's test scores. Perhaps secret rules of a game, or a hint for the next move. Those are examples of web pages a person might want to prevent printing.

One line of CSS can be used to do the trick.

@media print { * { display:none; } }

With that CSS effective, attempting to print the web page from the browser will print a blank sheet of paper.

A caveat is that someone determined to print the page could do a screenshot and then print the screenshot image. Folks less determined or less technically oriented will be stymied.

Expanded information on this subject:

To prevent a web page being printed, use the one line of CSS near the beginning of this article and the browser will be prevented from sending the web page data to the printer for printing.

Attempts to print the web page will yield only blank pages.

(This content first appeared in Possibilities newsletter.)


WillMasterBlog > Tips and Tricks

Multiple Launches With One Cron Schedule

When you have several scripts that need to run at a specific frequency — a daily run of a script to compile stats from a log file is one example — setting up one cron instead of many may save time and frustration.

What you do is install the script below. In a separate file, list the URLs of the scripts that shall be run. Set up cron to run the script every day (or other schedule). When the script below runs, it does an HTTP request to each URL in your list, one URL at a time.

Unlike cron, this system lets you run scripts at any public URL at any domain, whatever PHP or other scripts that a browser or bot can access. Cron requires scripts to be available on the same server where cron is set up.

Most hosting companies provide a dashboard method for you to set up cron for your script. If your hosting company does not, articles Cron and Using Cron have information you may be able to use (warning, highly technical).

Here is the source code. One customization is required, talked about further below.

<?php
/*
Run All URLs
Version 1.0
March 17, 2024
Will Bontrager Software LLC
https://www.willmaster.com/
*/

// Specify the location of the file with the URLs to run.
$FileWithURLs = 'subdirectory/urls2run.txt';
// End of customizations.

$listOfURLs = array();
if( file_exists($FileWithURLs) ) { $listOfURLs = file($FileWithURLs); }
else { echo "File $FileWithURLs not found."; }
foreach( $listOfURLs as $url)
{
   $url=trim($url);
   if( preg_match('!^https?://!i',$url) )
      { file_get_contents($url); }
}
echo 'OK';
?>

Customization —

The above script will look for a text file (see next paragraph) that contains URLs to run. The text file can be anywhere on the server that could contain web pages.

When you have decided where to put the text file and what to name it, then replace subdirectory/urls2run.txt with the file's location. The file is not required to have a .txt file name extension; it can have any extension you wish to give it or even no extension at all. In other words, it needs to be a text file but you can give it any file name you wish to give it.

Upload the above source code as script runURLs.php or other *.php file name. Make a note of its URL.

Put the URLs of scripts you want to run into the text file specified in runURLs.php (see further above). URLs in the text file should be one URL per line. Script runURLs.php will ignore any lines that do not begin with http:// or https://.

Note: Do not specify the URL of runURLs.php in the text file containing URLs to run. You would end up with an infinite loop.

Set up a cron schedule for runURLs.php. Whenever runURLs.php runs, it will run the URLs in your text file as HTTP requests.

This system can put any URL on the internet on a scheduled run. URLs can be added and removed by updating the text file.

(This content first appeared in Possibilities newsletter.)


WillMasterBlog > Tips and Tricks

Blocking Text Field Form Submission

A form with only one text field and the cursor in that field will submit when the Enter/Return key is tapped, even when the form has no submit button.

That's the way it used to be.

Yesterday, I found out that a form may submit even when the form has more than one text field. A client found the "bug".

I fixed it by telling the form not to submit unless it is done with JavaScript. That worked.

Here is an example. The form will not submit when Enter/Return is tapped within the text field. It will submit only when the button is tapped. Note: The form submits to example.com in a new browser tab.

A text input field:

Here is how it was done.

  1. Put an id value and onsubmit="return false" attribute into the form tag. Here is an example:

    <form id="MyForm" onsubmit="return false" method="post" action="https://example.com/page.php">
    

    The id="MyForm" id value will be used in the next step.

    The onsubmit="return false" attribute prevents the user from submitting the form with HTML methods. It can only submit with JavaScript.

  2. Changed the submit button into type="button" and added an onclick=… attribute. Here is an example:

    <input type="button" onclick="document.getElementById('MyForm').submit()" value="Pay Now">
    

    The type="button" attribute will react only with JavaScript.

    The onclick="document.getElementById('MyForm').submit()" attribute submits the form when the button is tapped. The submission is accomplished by using the form field's id="MyForm" id value and the JavaScript submit() function.

The element with an onclick=… attribute doesn't have to be a button. It could be an image or anything else that is clickable, within or outside the form area.

The above steps will prevent a form from submitting except when a button with an onclick=… attribute is tapped.

(This content first appeared in Possibilities newsletter.)


How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC