?
Solved

HTML - How to add a variable?

Posted on 2009-07-14
8
Medium Priority
?
309 Views
Last Modified: 2012-05-07
Hello,

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="http://172.1.1.5/cgi-bin/start">

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 "http://155.5.5.5/cgi-bin"  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?

THANKS
0
Comment
Question by:margarit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Expert Comment

by:xBellox
ID: 24849824
you can save your action URL (http://172.1.1.5/cgi-bin/start)  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

0
 
LVL 36

Expert Comment

by:Zyloch
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.
0
 
LVL 7

Expert Comment

by:ajkhalifa
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(){
      document.form5.action=v;
}
--------------------------------------------------------------------

and then in your code make it like this:


------------------------------------------------------------------------
<html>
<head>
<script src="myjsfile.js" type="text/javascript"></script>
</head>
<body onload="changeAction();">
<form name="form5" method="post">
<input type="submit">
</form>
</body>
</html>
------------------------------------------------------------------------
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:margarit
ID: 24857061
Hello,
Thanks for helpful comments!!!
xBellox:
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?
ajkhalifa:
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.
Zyloch:
Can you explain me the second solution in more details? Maybe you can give me an example.
THANKS
Margarit
0
 
LVL 36

Assisted Solution

by:Zyloch
Zyloch earned 400 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, http://www.google.com/search?hl=en&q=test&aq=f&oq=&aqi=g10 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:

<?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.
0
 

Author Comment

by:margarit
ID: 24858092
Hi,
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:
<?js
$form_action = "http://170.1.1.5.js';
?>
 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?
THANKS
Margarit
 
 
 
 
0
 
LVL 9

Accepted Solution

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

http://172.1.1.5/cgi-bin/start

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="http://172.1.1.5/cgi-bin/start">

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.
0
 

Author Comment

by:margarit
ID: 24859197
THANKS I will try it.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

718 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