homepage Welcome to WebmasterWorld Guest from 54.198.130.203
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Must Be A Cleverer Way?
Beginners Attempt at a PHP Site Update Script
michael heraghty




msg:1266165
 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.");
}

?>

 

TomAnthony




msg:1266166
 3:02 pm on Apr 5, 2006 (gmt 0)

Hrm, ok. Well there are a few issues, the first being the form your customer sees - are the fields blank or do they have the previous offer values in them?

I am guessing you are curently leaving them blank, so lets work with this. Now you will only want to update an offer if the form has something in that section.

Try this:


<?php
$filename = "offer1.txt";
$heading = $_POST['offersHeading'];
$body = $_POST['offersBody'];
$text = '<p><h2>'.$heading.'</h2>'.$body.'</p>';
$filename2 = "offer2.txt";
$heading2 = $_POST['offersHeading2'];
$body2 = $_POST['offersBody2'];
$text2 = '<p><h2>'.$heading2.'</h2>'.$body2.'</p>';
// if the heading was not empty, proceed with writing file.
if (!empty($heading))
{
$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
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 Special Offer 1. Please go back and try again.");
}
}
// if the heading was not empty, proceed with writing file.
if (!empty($heading2))
{
$fp2 = fopen ($filename2, "w"); # w = write to the file only, create file if it does not exist, discard existing content
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 Special Offer 2. Please go back and try again.");
}
}
?>

However, this does have a problem -- if the customer wants to have an offer 1, and for there to be no offer 2, then this code won't let them clear offer 2!

I'll leave sorting this problem out to you - give it a try and post again if you are struggling.

michael heraghty




msg:1266167
 3:57 pm on Apr 5, 2006 (gmt 0)

Simple but effective! Thanks TA, I appreciate it!

I am going to write a few lines of code that will enable "Remove Offer 1/Remove Offer 2" buttons on the form page.

Oh, and by the way, you guessed correctly -- I did not have any text populating the form fields.

michael heraghty




msg:1266168
 5:36 pm on Apr 5, 2006 (gmt 0)

Okay, I've been trying to add a couple of buttons that enhance the script, as you will see below.

It all works fine EXCEPT in cases when new data is sent to the form (from the page itself), this causes the entire script to run again. Forgive my lack of undesrtanding, as I'm new to programming. Hopefully someone can spot the error in this code and advise me:

<?php
$filename = "offer1.txt";
$heading = $_POST['offersHeading'];
$body = $_POST['offersBody'];
$text = '<p><h2>'.$heading.'</h2>'.$body.'</p>';
$filename2 = "offer2.txt";
$heading2 = $_POST['offersHeading2'];
$body2 = $_POST['offersBody2'];
$text2 = '<p><h2>'.$heading2.'</h2>'.$body2.'</p>';
$offer1status = $_POST['offer1Status'];
$offer2status = $_POST['offer2Status'];

// if the heading was not empty, proceed with writing file 1.
if (!empty($heading))
{
$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp)
{
fwrite ($fp, $text);
fclose ($fp);
echo ("<h2>Special Offer 1</h2>\n<strong>Special Offer 1</strong> has been updated. To review your updates, go to the <a href=\"special-offers.php\" target=\"_blank\">Special Offers</a> page.");
}
else
{
echo ("<h2>Special Offer 1</h2>\nThere was an error updating Special Offer 1. Please go back and try again.");
}
}

//if the heading was empty, check wheter the user wanted to remove offer 1
else
{
echo ("<p>You did not enter any text into Special Offer 1. Do you want to remove Special Offer 1 or leave it as it is?</p>\n
<form method=\"post\" action=".$_SERVER['PHP_SELF'].">\n
<input type=\"radio\" name=\"offer1Status\" value=\"Remove\">Remove Offer 1<br />\n
<input type=\"radio\" name=\"offer1Status\" value=\"Leave\">Leave Offer 1 as it is\n
<p><input type=\"Submit\" value=\"Update Special Offer 1\"></p>\n
</form>");
}

//if the user selects "Remove", write a blank file
if ($offer1status == 'Remove'){
$text = ' ';
$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp)
{
fwrite ($fp, $text);
fclose ($fp);
echo ("<h2>Special Offer 1</h2>\n<strong>Special Offer 1</strong> has been <strong>removed</strong>. To review, go to the <a href=\"special-offers.php\" target=\"_blank\">Special Offers</a> page.");
}
else
{
echo ("<h2>Special Offer 1</h2>There was an error removing Special Offer 1. Please go back and try again.");
}
}
elseif ($offer1status == 'Leave'){
echo ("<h2>Special Offer 1</h2>\n<p>You chose to leave Special Offer 1 as it is. To review your updates, go to the <a href=\"special-offers.php\" target=\"_blank\">Special Offers</a> page.</p>");
}

// if the heading was not empty, proceed with writing file 2.
if (!empty($heading2))
{
$fp2 = fopen ($filename2, "w"); # w = write to the file only, create file if it does not exist, discard existing content
if ($fp2)
{
fwrite ($fp2, $text2);
fclose ($fp2);
echo ("<h2>Special Offer 2</h2>\n<p><strong>Special Offer 2</strong> has been updated. To review your updates, go to the <a href=\"special-offers.php\"target=\"_blank\">Special Offers</a> page.</p>");
}
else
{
echo ("There was an error updating Special Offer 2. Please go back and try again.");
}
}

//if the heading was empty, check wheter the user wanted to remove offer 2
else
{
echo ("<h2>Special Offer 2</h2><p>You did not enter any text into Special Offer 2. Do you want to remove Special Offer 2 or leave it as it is?</p>\n
<form method=\"post\" action=".$_SERVER['PHP_SELF'].">\n
<input type=\"radio\" name=\"offer2Status\" value=\"Remove\">Remove Offer 2<br />\n
<input type=\"radio\" name=\"offer2Status\" value=\"Leave\">Leave Offer 2 as it is\n
<p><input type=\"Submit\" value=\"Update Special Offer 2\"></p>\n
</form>");
}

//if the user selects "Remove", write a blank file
if ($offer2status == 'Remove'){
$text = ' ';
$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp)
{
fwrite ($fp, $text);
fclose ($fp);
echo ("<h2>Special Offer 2</h2>\n<strong>Special Offer 2</strong> has been <strong>removed</strong>. To review, go to the <a href=\"special-offers.php\" target=\"_blank\">Special Offers</a> page.");
}
else
{
echo ("There was an error removing Special Offer 2. Please go back and try again.");
}
}
elseif ($offer2status == 'Leave'){
echo ("<h2>Special Offer 2</h2>\n<p>You chose to leave Special Offer 2 as it is. To review your updates, go to the <a href=\"special-offers.php\" target=\"_blank\">Special Offers</a> page.</p>");
}
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved