Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Must Be A Cleverer Way?


michael_heraghty - 2:20 pm on Apr 5, 2006 (gmt 0)


Hi All,

I wonder if anyone could help a PHP newbie?

I wanted to write a script that would allow a client of mine to update the special offers section of his website (it's for a small guesthouse).

Typically there are no more than two special offers at any time. What I did was create a password-protected form page for him, from where he can update the special offers. The form page passes the name of each special offer, and the body text to a page that processes the form output, the code for which is below.

This code then outputs results into two text files. The PHP page displaying the offers uses php includes to get the text from each file.

Okay, it's clunky but it works. The code is below. One major downside is that the "site has been updated" message always displays twice. Another downside is that the script always overwrites both output text files every time.

Can anyone suggest how I may improve this?

Code:
<?PHP

$filename = "offer1.txt";
$heading = $_POST['offersHeading'];
$body = $_POST['offersBody'];
$text = '<p><h2>'.$heading.'</h2>'.$body.'</p>';
$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents

$filename2 = "offer2.txt";
$heading2 = $_POST['offersHeading2'];
$body2 = $_POST['offersBody2'];
$text2 = '<p><h2>'.$heading2.'</h2>'.$body2.'</p>';
$fp2 = fopen ($filename2, "w"); # w = write to the file only, create file if it does not exist, discard existing content

if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
echo ("<strong>Special Offer Number 1</strong> has been updated. To review your updates, go to the <a href=\"special-offers.php\">Special Offers</a> page.");
}
else {
echo ("There was an error updating the website. Please go back and try again.");
}

if ($fp2) {
fwrite ($fp2, $text2);
fclose ($fp2);
echo ("<p><strong>Special Offer Number 2</strong> has been updated. To review your updates, go to the <a href=\"special-offers.php\">Special Offers</a> page.</p>");
}
else {
echo ("There was an error updating the website. Please go back and try again.");
}

?>


Thread source:: http://www.webmasterworld.com/php/12409.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com