I have two HTML pages: form.html and display.html. In form.html, there is a form:
<form action="display.html">
<input type="text" name="serialNumber" />
<input type="submit" value="Submit" />
</form>
The form data is sent to display.html. I want to display and use the form data serialNumber
in display.html, as shown below:
<body>
<div id="write">
<p>The serial number is: </p>
</div>
<script>
function show() {
document.getElementById("write").innerHTML = serialNumber;
}
</script>
</body>
So how can I pass the serialNumber
variable from form.html to display.html so that the above code in display.html will show the serial number and the JavaScript function show() gets the serialNumber
from the first HTML?
In display.html you should add the following code.
If your situation is as described; You have to send action to a different servelet from a single form and you have two submit buttons and you want to send each submit button to different pages,then your easiest answer is here.
For sending data to two servelets make one button as a submit and the other as button.On first button send action in your form tag as normal, but on the other button call a JavaScript function here you have to submit a form with same field but to different servelets then write another form tag after first close.declare same textboxes there but with hidden attribute.now when you insert data in first form then insert it in another hidden form with some javascript code.Write one function for the second button which submits the second form. Then when you click submit button it will perform the action in first form, if you click on button it will call function to submit second form with its action. Now your second form will be called to second servlet.
Here you can call two serveltes from the same html or jsp page with some mixed code of javascript
An EXAMPLE of second hidden form
Use "Get" Method to send the value of a particular field through the browser:
You need the get the values from the query string (since you dont have a method set, your using GET by default)
use the following tutorial.
http://papermashup.com/read-url-get-variables-withjavascript/
If you have no option to use server-side programming, such as PHP, you could use the query string, or GET parameters.
In the form, add a
method="GET"
attribute:When they submit this form, the user will be directed to an address which includes the
serialNumber
value as a parameter. Something like:You should then be able to parse the query string - which will contain the
serialNumber
parameter value - from JavaScript, using thewindow.location.search
value:See also JavaScript query string.
The alternative is to store the values in cookies when the form is submit and read them out of the cookies again once the
display.html
page loads.See also How to use JavaScript to fill a form on another page.