/    Sign up×
Community /Pin to ProfileBookmark

Fopen, write, on file questons and problem.

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 ?

[code=php]
<?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 .’nr’. $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>

[/code]

Thanks !
Ha a nice day !

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ChazzlMay 21.2008 — Can you simply append new information to [B]messages.html[/B]?

[code=php]$Handle = fopen($File, 'a+');[/code]
Copy linkTweet thisAlerts:
@ChazzlMay 21.2008 — Oooh, I might also suggest stripping tags out of the user input to prevent someone from abusing the shoutbox.

Use strip_tags ( string str [, string allowable_tags] ) or htmlentities ( string string )


[code=php]$messageq = htmlentities($_POST["messageq"]);[/code]
×

Success!

Help @Peuplarchie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...