When using a web server as a back-end to an HTML page, for example filling in a form, the result from the back-end can be
- “Redirect to a new page”.
- A stream of HTML. This is an HTML document where the “boilerplate” or constant data, is intermixed with the variable data included inline. The HTML is displayed, and replaces the previous page.
- A stream of data containing data such as changed fields. The requesting page can use this stream to update its page.
Redirect to a new page
The server can send back status meaning redirect
#!/usr/bin/python3
import cgitb
import cgi
import sys, os, io
cgitb.enable(display=0, logdir="/home/colinpaice/first.log")
print('Status: 303 See Other2')
print("Content-Type: text/html")
// the url and any parmeters
print("Location: ../c3.html?colin=p1&error=p2")
# needs blank line to say end of headers
print()
This causes page c3.html to be displayed. The page can process the URL and process the options passed on the url.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML lang="en">
<HEAD>
<script>
// this function is invoked on page load
function js_onload_code (e){
var url = document.location.href;
// split the parameters from the url base
var p = url.split('?')[1]
if (p)
{
var params = p.split('&');
if (params)
{
var l = params.length;
// display what was passed in
alert("colin:"+url);
for (var i = 0 ; i < l; i++)
{
// split it into kw"="value
tmp = params[i].split('=');
document.write(tmp[0] + "=" + tmp[1] +"!<br>");
}
} // if params
} // if
}
// this causes the above script to be executed.
window.onload= js_onload_code ();
</script>
<BODY >
<P>This is page c3.html
</BODY>
</HTML>
This displayed a page with url http://localhost/c3.html?colin=p1&error=p2
colin=p1! error=p2! This is page c3.html
If you repeatedly refresh this page you will get more and more data! It is better to use a field, then clear it before adding the information.
2 thoughts on “Updating a web page from a server – redirect to a new page.”