Some Basic CGI Tips

From PlainOldWebserver

Jump to: navigation, search

POW is what I (http://www.scottwickham.com) have been looking for. A tool to allow me to develop websites that run on my local machine. So instead of using REBOL, PHP-GTK, TrueBasic, RealBasic, etc to make my client side applications I can use what I know. HTML and Javascript.


The Second thing I tried with POW is build a cgi. Here is what you need to know.


Example HTML file with Forms: echo.html

<html>
<head>
</head>
<body>


<h4>Get Form:</h4>
<hr>
<form  method='Get' action='get.sjs'> 
First Name:<input size="20" name="fname"><br>
Last Name:<input size="20" name="lname" ><br>
<INPUT TYPE='SUBMIT' VALUE='echo'></form>
 
<h4>Post Form:</h4>
<hr>
<form  method='Post' action='post.sjs'> 
First Name:<input size="20" name="fname"><br>
Last Name:<input size="20" name="lname" ><br>
<INPUT TYPE='SUBMIT' VALUE='echo'></form>
 

<h4>Save Text Area Form:</h4>
<hr>
<form  method='Post' action='savetext.sjs'> 
<TEXTAREA  name='filedata'  ROWS=3 COLS=40 WRAP></TEXTAREA><br>
<INPUT TYPE='SUBMIT' VALUE='echo'></form>
</body>
</html>


The CGI Pages get.sjs

 
<?sjs

// This gets the variables from the form in echo.html 
fname   =  pow_server.GET['fname'];
lname   =  pow_server.GET['lname']; 
// these lines clear up the urlencoded variables that were returned  
fname =  unescape(fname.replace(/\+/g," "));
lname =  unescape(lname.replace(/\+/g," ")); 

out = "Echo of the Form:  "+ fname+"  "+lname;
// Output the string to the webbrowser

document.write(out);
                            
        ?>


The CGI Pages post.sjs

 
<?sjs
first   =  pow_server.POST['fname'];
last   =  pow_server.POST['lname']; 
first =  unescape(first.replace(/\+/g," "));
last =  unescape(last.replace(/\+/g," ")); 
out = "Echo of the Form:  "+ first+"  "+last
?>
<html>
<head></head>
<body>Put Your HTML Here</BR>


<?sjs
//putting dynamic content into what is mostly and html page
document.write(out);
?>
</body>
</html>


The CGI Pages savetext.sjs


<?sjs 
filedata   =  pow_server.POST['filedata']; 
filedata =  unescape(filedata.replace(/\+/g," "));
//Save  the form are information to a file named file2.txt 
pow_file_put_contents("file2.txt", filedata, "w" );
// Build dynamic string using  javascript 
out = "Echo of the Form: <br><table border=2><tr><td> "+ filedata + "</td></tr></table>"; 
?>
<html>
<head>
</head>
<body>
Put you Html header Here </BR>
<?sjs
 
 document.write(out);
 
 ?>
</body>
</html>




So there you have it Three Basic Form, and Three different cgi pages. Using Text and TextArea inputs.



by <a href="http://www.scottwickham.com">Scott Wickham</a>


Personal tools
Navigation