Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HTML - How to add a variable?

Posted on 2009-07-14
8
Medium Priority
?
311 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

782 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