Heres my script:

[FONT=Courier New]<body>

<?php
$shouts = fopen ( "shouts.txt" , "a+" );

fwrite ( $shouts , "<b>$POST[name]:</b> $POST[message]<br>\r\n" );

readfile ("shouts.txt");
?>

<form action="index.php" method="POST">
Name!<br>
<input type="text" name="name"><br>
Message!<br>
<input type="text" name="message"><br>
<input type="submit" value="Shout!">

</body>[/FONT]

My problem is that when the page is first loaded it writes a blank message to the text file (just a : colon). I understand why it does this, but how can I make the script so it only fwrites when a message has been entered? Or so it doesnt fwrite on first load...?

Any help appreciated, Thanks

    You could try: -

    if(isset($_POST[name]) && isset($_POST[message])) {
        fwrite ( $shouts , "<b>$_POST[name]:</b> $_POST[message]<br>\r\n" );
    }
    

    assuming isset is the best way of doing it.. I'm never sure whats best, sometimes I end up doing isset($var) && $var!=null.. which probably does the same thing twice, depends on the situations though..

      ah-ha yes it worked thanks 😃

      Could this

      [FONT=Courier New]if(isset($_POST[message]))[/FONT]

      be changed so that name and message have to both be filled in?

        if(isset($_POST['mesage']) && isset($_POST['name']))

        Oh, wait, that's pretty much what benbox wrote.

          [FONT=Courier New]if(isset($POST[name]) && ($POST[message]))[/FONT]

          This half works, [FONT=Courier New]$POST[message][/FONT] has to be set but [FONT=Courier New]$POST[name][/FONT] doesnt 🙁

            AHH ive got it,

            if($POST[name] && ($POST[message]) > null)

            just had to say they both equal greater than null... i dont know why this didnt work when it said they both had to be set... Oh well, Thanks for all help!

              samsynnyuk wrote:

              This half works

              That's because you only half wrote it.

                Write a Reply...