HTML - How to add a variable?

Posted on 2009-07-14
Last Modified: 2012-05-07

I am new to HTML :
I have built my website from several html pages. Each html page includes a form.
Each form defined like this:
<form class="MyForm" method="get" action="">

I want to set the action as a parameter/const outside my pages, and to read this parameter from some place. Because if tomorrow I decide to change the action to be ""  I should go to each page and change it their. Instead I want to change this parameter on one place only.
How can I do it?

Question by:margarit
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 24849824
you can save your action URL (  as a normal text file named: "action.include" or anything else.

And in your HTML forms you write this:

<form class="MyForm" method="get" action="<!--#include virtual="action.include" -->">

Open in new window

LVL 36

Expert Comment

ID: 24849854
Off the top of my head there are 3 solutions,

1) If all your forms are the same, you can include it in a single HTML page that is called by an iframe. You can even make the iframe look like part of the page with appropriate CSS styles.

2) You can employ a server-side language, such as PHP, which will print out the HTML for you, substituting a variable that you define elsewhere in a file included in your PHP pages for the form action.

3) You can finagle your way around it by setting the form action from an external JavaScript file. This would  work for all browsers that have JavaScript enabled, but because of that unnecessary precondition given the other two solutions, I do not recommend it.

Let us know if you need any clarification on any of these.

Expert Comment

ID: 24850527
It is always better to use server side programming language for that such as PHP, ASP, ASP.NET, JSP ... etc.
but if you are only in html environment, you can use javascript...
create a dot js file and define a variable say and make a change action function like this:
var v='http://myip/a.asp';
function changeAction(){

and then in your code make it like this:

<script src="myjsfile.js" type="text/javascript"></script>
<body onload="changeAction();">
<form name="form5" method="post">
<input type="submit">

Author Comment

ID: 24857061
Thanks for helpful comments!!!
How should I put (define) URL in my text file? Can you please give me an example? And If I want to add several parameters to one text file, can I do it?
I can put these way several scripts to one external file? What is a diffrence between methods "get" & "host". I read in the web but did not understand.
Can you explain me the second solution in more details? Maybe you can give me an example.
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

LVL 36

Assisted Solution

Zyloch earned 100 total points
ID: 24857109
First, in xBellox's solution, a server side include (SSI) is used. Your server or web host needs to support server side includes for it to work. I believe, if it does, you can just put your form action URL in a file called action.include by itself. I don't exactly remember the difference between virtual and file, but you can look that up to find out where to place the file -- probably in the same directly as your HTML.

Second, the two form methods GET and POST are both used to transmit information from a form submit. A get request will show up in the URL as a query string. For example, is a Google search that is passed from a GET form -- note the key=value pairs after the question mark. POST on the other hand passes information that does not appear in the URL and essentially must be submitted from the form instead of merely entered into the URL (this is not exactly true, but it's good enough for now). The most important thing you should remember perhaps is that GET has a limit on how much form information can be passed, and POST practically does not.

Finally, my second solution is similar to the solution involving SSI in the sense that it requires your server or web host to support a server side language like PHP (or ASP or JSP). If it supports PHP, you could write into yourpage.php:

$form_action = 'blah blah.php';
// All your HTML
<form action="<?php echo $form_action; ?>">

Of course, this is not so useful unless  you put the URL in a third file and include it in this file with PHP, that's possible, of course, but it would be pointless to give it to you if you can't employ this solution. Let us know your situation.

Author Comment

ID: 24858092
THANKS for  the explanations! They are very helpful!
My server supports Java script. If I understand right.
I create file "myDefinitions.js":
Inside I put the following:
$form_action = "';
 And in my HTML:
<form action="<?js echo $form_action; ?>">
Is my syntax correct?
How does my form know to find form_action variable in  "myDefinitions.js" file?

Accepted Solution

xBellox earned 400 total points
ID: 24858302
Create a new document text, write your action URL as normal text like:

And save it with any name in same directory of your HTML file.

Then in your HTML files, at your form declaration just "include" the file with URL name.

For example, suppose you save your file with the name "action.txt", your form will be like this

<form class="MyForm" method="get" action="<!--#include virtual="action.txt" -->">

Then the brower will understand it and load the file action.txt, so it will equivalent to  this:

<form class="MyForm" method="get" action="">

It occurs because the command <!--#include virtual="action.txt" --> makes the browser read all content of the file action.txt and put it in code.

Author Comment

ID: 24859197
THANKS I will try it.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
After moving to webstite page stopped working 7 85
simplest php form 3 63
Objects on Same Line 2 18
How can I increase the size of this logo 5 22
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now