...

View Full Version : Fopen, write, on file questons and problem.



Peuplarchie
05-21-2008, 03:16 AM
Good day to you all,
I have a piece of code for a little shootbox on single page.

So you have a form, when a message is submit, the code write it to a file; messages.html.

My problem with this part is that the way I use, take the whole content of the html page add to it, at the bottom, the result of the submit PLUS another copy of that result, so it's duplicating all the time ???
I have tried different fopen modes.

Second : I need to have some code at the begining of the html file, how can I not overwrite it when I copy the meesage into the html file ?




<?php
if (isset($_POST['nameq'])) {

if ($_POST['nameq'] != "" && $_POST['messageq'] != "")
{
$nom = $_POST["nameq"];
$nomcorrecting = strtoupper($nom);
$nomcorr = substr($nomcorrecting, 0, -9);
$messageq = $_POST["messageq"];
$choice = $_POST["COLOUR_CHOICE"];



$nomcoul = "<font color=\"#".$choice."\" size=\"3\">".$nomcorr."</font> <br/>".$messageq."<br/><br/>";



$File = date('Y')."/".date('m')."/".date('d')."/messages.html";
$Handle = fopen($File, 'r+');
if ($Handle) {
$buffer = fgets($Handle, 4096);


$Data = $nomcoul .'\n\r'. $buffer;

fwrite($Handle, $Data);

fclose($Handle);


}
}
}
?>



<center>
<h3>Nouveau message...</h3>

<iframe id="myframe" name="myframe" src="<? echo date('Y').'/'.date('m').'/'.date('d').'/messages.html'; ?>" scrolling="auto" marginwidth="0" marginheight="0" frameborder="0" style=" width:580px; height:400px;"></iframe>


<form action="" method="post">
<input type="hidden" name="nameq" value="<? echo $_SESSION['Name'];?>"/>
<textarea name="messageq" cols="70" rows="5"></textarea><br/>
<b>Couleur du text : </b>
<?
echo "<select name=\"COLOUR_CHOICE\" id=\"COLOUR_CHOICE\">";

$colourArray = array("000000", "cccc99", "FFFFFF", "0000FF");
for ($i = 0; $i < count($colourArray); $i++)
{
$selected = ($colourArray[$i] == trim($_POST['COLOUR_CHOICE'])) ? 'selected="selected"' : '';
echo '<option value="' . $colourArray[$i] . '"' . $selected . ' . id="opt' . $colourArray[$i] . '">#' . $colourArray[$i] . '</option>';
}
echo "</select>";
?>

<input type="submit" name="Envoyer !!!" value="post"/>
</form>





Thanks !
Ha a nice day !

mlseim
05-21-2008, 05:40 AM
I'm trying to figure out why you don't just append text onto a text file
and use PHP include to display that text file on your web page.

mlseim
05-21-2008, 05:47 AM
Maybe something like this?

My test doesn't use your session variable, and I'm only using "messages.html".



<?php
if (isset($_POST['nameq'])) {

if ($_POST['nameq'] != "" && $_POST['messageq'] != "")
{
$nom = $_POST["nameq"];
$nomcorrecting = strtoupper($nom);
$nomcorr = substr($nomcorrecting, 0, -9);
$messageq = $_POST["messageq"];
$choice = $_POST["COLOUR_CHOICE"];

$nomcoul = "<font color=\"#".$choice."\" size=\"3\">".$nomcorr."</font> <br/>".$messageq."<br/><br/>";

$File = "messages.html";
$Handle = fopen($File, 'a+');
fwrite($Handle, $nomcoul);
fclose($Handle);
}
}
?>

<center>
<h3>Nouveau message...</h3>

<iframe id="myframe" name="myframe" src="messages.html" scrolling="auto" marginwidth="0" marginheight="0" frameborder="0" style=" width:580px; height:400px;"></iframe>


<form action="" method="post">
<input type="hidden" name="nameq" value="123"/>
<textarea name="messageq" cols="70" rows="5"></textarea><br/>
<b>Couleur du text : </b>
<?
echo "<select name=\"COLOUR_CHOICE\" id=\"COLOUR_CHOICE\">";

$colourArray = array("000000", "cccc99", "FFFFFF", "0000FF");
for ($i = 0; $i < count($colourArray); $i++)
{
$selected = ($colourArray[$i] == trim($_POST['COLOUR_CHOICE'])) ? 'selected="selected"' : '';
echo '<option value="' . $colourArray[$i] . '"' . $selected . ' . id="opt' . $colourArray[$i] . '">#' . $colourArray[$i] . '</option>';
}
echo "</select>";
?>

<input type="submit" name="Envoyer !!!" value="post"/>
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum