...

View Full Version : Functionality that allows someone to update a text blurb



AshleyQuick
07-29-2003, 05:58 PM
Can somone explain how to implement a web-based script that will allow someone to use a page to enter text that will be displayed (in a designated area) on a home page?

In other words, I want to be able to update an "Events" section without having to edit the html code.

Ash

oracleguy
07-29-2003, 06:27 PM
You need to have a database or a text file that would contain the content. Then using some server side language, you would have to display it on the homepage and write another page that displays the text, and lets someone edit it. Then it saves it back into the database/file.

AshleyQuick
07-29-2003, 06:30 PM
Ugh. Do you know of any tutorials that would explain the procedure?

ash

oracleguy
07-29-2003, 10:18 PM
Not off hand, but the code is pretty simple. Which language(s) do you have available to you? ASP, PHP, etc.

AshleyQuick
07-29-2003, 10:32 PM
PHP

Waldo
07-30-2003, 04:13 AM
Here's a quick-and-dirty implement. It is very unsecure and isn't as automated as I might like it to be (e.g., create file if non-existent, modify permissions, etc.), but I kept getting errors with those included:

Take the page you want the information on and put this in it where you want the text to go:

<?php include('ev_data.txt'); ?>

Create the file 'ev_data.txt' and put it in the same directory. chmod it to 0666 using an FTP program. Create a new PHP file (preferably with an unguessable filename) and put it in the same directory as the page you just edited. Then just run this file to update the text:


<?php
$data_file='ev_data.txt';
$changes='';

if(isset($contents)) {
$archive=fopen($data_file,'r+');
flock($archive,2);
ftruncate($archive,0);
fputs($archive,$contents);
flock($archive,3);
fclose($archive);
unset($contents);
$changes='<b>Changes made!</b><br />';
}

function getfile($filename) {
$fd = fopen($filename, "rb");
$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}

$data=getfile($data_file);
?>
<html>
<head><title>Edit Events:</title></head>
<body>
<?php echo $changes; ?>
<form name="editform" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Edit Events information here:<br />
<textarea name="contents" rows="20" cols="80" tabindex="2"><?php echo $data;?></textarea><br />
<input type="submit" name="submitdata" value="Submit changed data" />&nbsp; &nbsp;<input type="reset" name="resetdata" value="Erase data - I'm starting over!" /></form><br />

Note: data will be unchanged until the "Submit changed data" button is clicked.
</body></html>


Anyone else, feel free to post better versions - chmod and fopen kept erroring on me, so creating/chmodding is still a manual job.

Jeff Walden

AshleyQuick
07-30-2003, 04:54 PM
That works beautifully! Now, to make some minor layout modifications...

Thanks!

Ash

AshleyQuick
07-30-2003, 05:43 PM
Actually, on second thought, I want to be a nag. :)

How would you implement a method to preview the content/text before it's submitted? Maybe a pop-up window that will show how it looks in html?

Pretty please! :)

Ash

Waldo
07-31-2003, 12:39 AM
<?php
$data_file='ev_data.txt';
$changes='';

if(isset($contents)) {
$archive=fopen($data_file,'r+');
flock($archive,2);
ftruncate($archive,0);
fputs($archive,$contents);
flock($archive,3);
fclose($archive);
unset($contents);
$changes='<b>Changes made!</b><br />';
}

function getfile($filename) {
$fd = fopen($filename, "rb");
$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}

$data=getfile($data_file);
?>
<html>
<head>
<title>Edit Events:</title>
<script language="Javascript" type="text/javascript"><!--
function view() {
q=open("","l","status=0,toolbar=0,width=500,height=500,resizable=1,menubar=1,location=0");
q.document.open();
q.document.write(document.forms[0].comments.value);
q.document.close();
}

//--></script>
</head>
<body>
<?php echo $changes; ?>
<form name="editform" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Edit Events information here:<br />
<textarea name="contents" rows="20" cols="80" tabindex="2"><?php echo $data;?></textarea><br />
<input type="button" onclick="view()" />&nbsp; &nbsp;<input type="submit" name="submitdata" value="Submit changed data" />&nbsp; &nbsp;<input type="reset" name="resetdata" value="Erase data - I'm starting over!" /></form><br />

Note: data will be unchanged until the "Submit changed data" button is clicked.
</body></html>


That'll do it. It's not PHP (really Javascript), but you
use the best tool for the job. By the way, this brings up
an important point: the code that goes in must be HTML code.
New paragraphs will show up as spaces only because that's
how HTML works.

AshleyQuick
07-31-2003, 06:35 AM
Although, I'm getting a js error. :(

http://mywebpages.comcast.net/maxpixel/js_error.gif

Spudhead
07-31-2003, 10:27 AM
I'm going to hazard a guess that "document.forms[0].comments.value is not an object" because there isn't anything on the page called "comments".

It may be that "document.editform.contents.text" is more successful.

AshleyQuick
07-31-2003, 02:25 PM
Hmm...I tried document.editform.contents.text and I'm still getting a js error. I've reached an impasse...what else could it be?

Ash

Waldo
07-31-2003, 06:07 PM
Oops, copied it from another script and forgot to change the field. The line that looks like this:


q.document.write(document.forms[0].comments.value);

should really be this:


q.document.write(document.forms[0].contents.value);

That should work a little better ;-).

Jeff

AshleyQuick
07-31-2003, 07:24 PM
Awesome! Works beautifully!

Is there a way to get that pop-up to use the style sheet?

Reason I ask...the place in which the text is populating has a style/class attached to it:

<td class="news"><?php include('news/ev_data.txt'); ?></td>

If this is too much of a pain, I understand. At least I can see if everything is formatted properly. Being able to see the styles would be a bonus! :)

Ash

oracleguy
07-31-2003, 09:30 PM
I'm not sure about this but you might be able to simply change the script slightly.


<script language="Javascript" type="text/javascript"><!--
function view() {
q=open("","l"," status=0,toolbar=0,width=500,height=500,resizable=1,menubar=1,location=0");
q.document.open();
q.document.write('<link rel=stylesheet href=default.css type=text/css>');
q.document.write(document.forms[0].comments.value);
q.document.close();
}

//--></script>

AshleyQuick
07-31-2003, 10:30 PM
Good try! I was thinking you were onto something. Unfortunately, that didn't work though. :(

Ash

Waldo
07-31-2003, 11:49 PM
Perhaps the <link> tag has to be in the head tags? I think I might have read this somewhere, though I couldn't remember if my life depended on it. Try moving it into the <head></head> area (making sure it's not in the middle of another tag) and see if that works. Also, make sure the URL for the stylesheet is correctly pointed; the way you have it only works if default.css is in the same directory as every other file we've mentioned.

As for previewing it as part of the main page, I can't think of a simple way to do it without writing a bit more code. Perhaps copying your index page and all the other pages into a new directory and then fiddling all you want until it looks okay, then just copying text from the preview file to the real file might work? It's a lot of work, but I can't really think of anything easy.

Jeff

(After-edit: the Javascript just copies the text box to the HTML popup - that wouldn't work. Try using these lines in place of the q.document.write(document.forms[0].comments.value); line:

q.document.write('<html><head>');
q.document.write( --- INSERT LINK CODE HERE);
q.document.write('</head><body>');
q.document.write(document.forms[0].comments.value);
q.document.write('</body></html>');

AshleyQuick
08-01-2003, 02:22 AM
Unbelievable...it works perfectly! This has surpassed my expectations immensely!

Thank you to everyone for your input. A special thanks to Waldo for your expertise! :p:D


Ash



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum