Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post

    Post 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 ?


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

    if (
    $_POST['nameq'] != "" && $_POST['messageq'] != "")
    {
    $nom $_POST["nameq"];
    $nomcorrecting strtoupper($nom);
    $nomcorr substr($nomcorrecting0, -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($Handle4096);


    $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 !

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Maybe something like this?

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

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

    if (
    $_POST['nameq'] != "" && $_POST['messageq'] != ""

    $nom $_POST["nameq"]; 
    $nomcorrecting strtoupper($nom); 
    $nomcorr substr($nomcorrecting0, -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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •