I was asked by a visitor how he could make a button, when clicked, run a script. In his case, he wanted the button to run a Python script, but you can make the button run any server-side script, such as PHP, Perl, Ruby, Ruby on Rails, Python, etc.
You will need some HTML knowledge to use this tutorial. You don't have to be an expert, but you will at least need to know how to insert HTML code into your web page.
This tutorial merely deals with the HTML aspect of things, since that was the question that my visitor asked. It assumes that you already have the script that you want to "attach" (loosely-speaking) to the button. The script should be a server-side one, that is, it should be a program that runs on your web server (the machine on which your website is placed). This is typically a script written in PHP, Perl, Python, Ruby, Ruby on Rails, and any program that is executed as a CGI script regardless of the language they were written in.
Note that you do not need to do the following if all you want is for your button to run some JavaScript code. For such things, see the main button tutorial, where an example of a button executing a JavaScript function is given. JavaScript code runs on your visitor's computer, not the web server.
Since my visitor merely wanted to invoke a script, without passing it any additional information, there are multiple ways to do it. This article describes two.
One way to cause a button to invoke a script is to put it within a form, since that is the latter's raison d'étre.
Let's say that you want your button to run a script called "http://www.example.com/do-something.php
".
The HTML code for this is:
When clicked, the web browser will issue a request for the specified URL (ie, web address), in this case,
"http://www.example.com/do-something.php
". This, in turn, will cause the web server to execute
"do-something.php".
You should, of course, change the part that says "http://www.example.com/do-something.php
"
to point to the actual URL (ie, web address) of your script. Since you are not submitting any
form content, you can leave the method
attribute set to "get", although changing it to "post"
will do no harm. (The attribute only affects the way the script is handed form data. Since there is no form data
here, it makes no difference which method is used.)
In fact, you don't even need a form if you only want to invoke a script without submitting any other information. You can code it as a simple hyperlink, like so:
Then make the link look like a button using CSS. This can be done in any number of ways. See How to Create 3D Buttons Using CSS for more information. If you want to see an example of a link that looks like a button, just glance to the left column of this article (or in fact, any page on thesitewizard.com). All the buttons in that column are merely hyperlinks that have their appearance altered.
Copyright © 2019-2020 Christopher Heng. All rights reserved.
Get more free tips and articles like this,
on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
It will appear on your page as:
How to Make a Button Run a Server-Side Script (eg, a PHP, Perl, or Python Script) (HTML)