PDA

View Full Version : php form to generate a html document


andrewsheldon98
07-18-2002, 05:57 PM
is there anyway i can use a php form to be able to generate a html file depending on what you want to call the filename, and be able to write the code in below. it has to be placed in the same directory as where the php form is.

<HTML>
<HEAD>
</HEAD>
<BODY leftMargin=0 topMargin=0 link="#000080" vlink="#000080" alink="#000080" bgcolor="#CCFF99">
<table border="1" cellpadding="0" cellspacing="0" width="100%" height="100%" bordercolor="#000000" bgcolor="#CCFF99">
<tr>
<td width="100%" valign="top">
&nbsp;
<div align="center">
<table border="0" cellpadding="3" width="400" background="../topmenu.gif" height="30">
<tr>
<td>
<p align="center"><font color="#FFFFFF" size="2" face="Arial"><b>General Pool</b></font></td>
</tr>
</table>
</div>
&nbsp;
<div align="center">
<TABLE cellSpacing=0 cellPadding=0 border=1 width="440" bordercolor="#555576">
<TBODY>
<tr>
<TD bgColor=#000000 width="317">
<p align="left"><font color="#ffffff" face="Verdana" size="1"><b>Post&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="postnewtopic1.htm"><img border="0" src="postnew.gif" width="86" height="19"></a></b></font></p>
</TD>
<TD bgColor=#000000 width="123">
<p align="left"><font color="#ffffff" face="Verdana" size="1"><b>Poster</b></font></p>
</TD>
</tr>

Spookster
07-20-2002, 04:18 PM
Of course you can. Is the form going to contain the contents for this new html file? What is the new file suppose to be called?

andrewsheldon98
07-20-2002, 04:39 PM
no the form will only contain the output filename of the html specified by the user.

i want the php or cgi script to then write what is below after creating the blank document with the user specified filename:

<HTML>
<HEAD>
</HEAD>
<BODY leftMargin=0 topMargin=0 link="#000080" vlink="#000080" alink="#000080" bgcolor="#CCFF99">
<table border="1" cellpadding="0" cellspacing="0" width="100%" height="100%" bordercolor="#000000" bgcolor="#CCFF99">
<tr>
<td width="100%" valign="top">

<div align="center">
<table border="0" cellpadding="3" width="400" background="../topmenu.gif" height="30">
<tr>
<td>
<p align="center"><font color="#FFFFFF" size="2" face="Arial"><b>General Pool</b></font></td>
</tr>
</table>
</div>

<div align="center">
<TABLE cellSpacing=0 cellPadding=0 border=1 width="440" bordercolor="#555576">
<TBODY>
<tr>
<TD bgColor=#000000 width="317">
<p align="left"><font color="#ffffff" face="Verdana" size="1"><b>Post
<a href="postnewtopic1.htm"><img border="0" src="postnew.gif" width="86" height="19"></a></b></font></p>
</TD>
<TD bgColor=#000000 width="123">
<p align="left"><font color="#ffffff" face="Verdana" size="1"><b>Poster</b></font></p>
</TD>
</tr>

Spookster
07-20-2002, 05:35 PM
You can do this several different ways. Here is one example:

Create a php file that will actually create the html file:


<?php
$file_name = $HTTP_POST_VARS["new_file"];

$contents = <<<EOD


<HTML>
<HEAD>
</HEAD>
<BODY leftMargin=0 topMargin=0 link="#000080" vlink="#000080" alink="#000080" bgcolor="#CCFF99">
<table border="1" cellpadding="0" cellspacing="0" width="100%" height="100%" bordercolor="#000000" bgcolor="#CCFF99">
<tr>
<td width="100%" valign="top">

<div align="center">
<table border="0" cellpadding="3" width="400" background="../topmenu.gif" height="30">
<tr>
<td>
<p align="center"><font color="#FFFFFF" size="2" face="Arial"><b>General Pool</b></font></td>
</tr>
</table>
</div>

<div align="center">
<TABLE cellSpacing=0 cellPadding=0 border=1 width="440" bordercolor="#555576">
<TBODY>
<tr>
<TD bgColor=#000000 width="317">
<p align="left"><font color="#ffffff" face="Verdana" size="1"><b>Post
<a href="postnewtopic1.htm"><img border="0" src="postnew.gif" width="86" height="19"></a></b></font></p>
</TD>
<TD bgColor=#000000 width="123">
<p align="left"><font color="#ffffff" face="Verdana" size="1"><b>Poster</b></font></p>
</TD>
</tr>


EOD;

$file_pointer = fopen($file_name, "w");
$lock = flock($file_pointer, LOCK_EX);

if ($lock){
fputs($file_pointer, $contents);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);

?>


and then your html form that will supply the file name:


<form name="form1" method="post" action="create_file.php">
<input type="text" name="new_file">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>


and that will simply create the file with that file name with the contents provided in the contents variable.

Note that im using the herdoc syntax which I believe is supported only in php4+. Of course im assuming your host is using php4+ as they should be. :)

andrewsheldon98
07-20-2002, 08:49 PM
how do i then get the php script to go to the new file just created?


ive also tried to chmod the file but permission is denied? is it already chmod to 777?

Spookster
07-20-2002, 09:57 PM
Ok I should have guessed you would ask that. Here is a way to redirect to their page:


<?php
$is_submitted = $HTTP_POST_VARS["submit"];
$file_name = $HTTP_POST_VARS["new_file"];

if(!isset($is_submitted)){
?>

<html>
<head>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<input type="text" name="new_file">
<br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

<?php
}
else{

$contents = <<<EOD

<HTML>
<HEAD>
</HEAD>
<BODY leftMargin=0 topMargin=0 link="#000080" vlink="#000080" alink="#000080" bgcolor="#CCFF99">
<table border="1" cellpadding="0" cellspacing="0" width="100%" height="100%" bordercolor="#000000" bgcolor="#CCFF99">
<tr>
<td width="100%" valign="top">

<div align="center">
<table border="0" cellpadding="3" width="400" background="../topmenu.gif" height="30">
<tr>
<td>
<p align="center"><font color="#FFFFFF" size="2" face="Arial"><b>General Pool</b></font></td>
</tr>
</table>
</div>

<div align="center">
<TABLE cellSpacing=0 cellPadding=0 border=1 width="440" bordercolor="#555576">
<TBODY>
<tr>
<TD bgColor=#000000 width="317">
<p align="left"><font color="#ffffff" face="Verdana" size="1"><b>Post
<a href="postnewtopic1.htm"><img border="0" src="postnew.gif" width="86" height="19"></a></b></font></p>
</TD>
<TD bgColor=#000000 width="123">
<p align="left"><font color="#ffffff" face="Verdana" size="1"><b>Poster</b></font></p>
</TD>
</tr>


EOD;

$file_pointer = fopen($file_name, "w");
$lock = flock($file_pointer, LOCK_EX);

if ($lock){
fputs($file_pointer, $contents);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);


echo <<<EOD

<html>
<head>
<META HTTP-EQUIV="Refresh" Content="3; URL=$file_name">
</head>
<body>
Loading your page please wait...
</body>
</html>

EOD;
}
?>



Basically I combined the two files into one and then printed out a meta refresh tag to redirect the user to the page they just created.

Spookster
07-21-2002, 03:43 AM
your'e welcome.

andrewsheldon98
07-21-2002, 07:39 AM
cheers spookster, nice one

zhggu
12-02-2005, 08:06 AM
hi there,
i want your help,
i want php script for store my all forum into any txt file on ptf i m new in php so i cant understend that how i can do it or how it posible,
please help me about it
thinking full to you
zhggu