FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


News script





DjMilez
Hi ya'll, I'm new to php and I'm trying to work on a flat file news system.

So far, I have this script. I used some of it from another source.
Code:
<?php
if (!$_POST['submit']) {
echo("<h2>Enter a news item</h2>
<form method='post' action='$_SERVER[PHP_SELF]'>
User:<br>
<input type='text' name='user' size='30'>Administrator</input><p>
News item:<br>
<textarea cols='70' rows='8' name='news'></textarea><p>
<input type='submit' name='submit' value='Add News' /></form>");
exit;
}
else
{
$user = $_POST['user'];
$news = $_POST['news'];
$errors = array(); //array for errors
        if (empty($user)) { //if host is empty
            $errors[] = "You Must Enter A User Name!"; //lol
        } //end host check
        if (empty($news)) { //user check
            $errors[] = "You Must Enter A News Article!"; //you fail o_0//
        } //end user check
        if (count($errors) > 0) { //if theres more then 0 errors
            print "<h2>Errors Found!</h2>
            <p>"; //error header
            foreach ($errors as $err) { //foreach error
                print $err . '';
            } //end loop
            print "</p>";
            exit();
            if (!$cerror && !$derror) {
$conf_handle = fopen('shownews.php', 'w+');
                $data = "<table width='100%'><tr><td>Posted by $user </td><td width='80%'> $news </a>";
                $write = @fwrite($conf_handle, $data);
                if ($write) {
                    print "<h2>Success</h2>
                        <p>
                        Your news was added.
                        </p>";
}
?>


I want it to open and write to shownews.php once you submit the form, but when I visit the page it's just shows up blank.
What am I doing wrong? And... is there a way to improve it?

I'm sorry if this is a n00bie question.
rvec
Code:
<?php
if (!$_POST['submit']) {
   echo("<h2>Enter a news item</h2>
   <form method='post' action='$_SERVER[PHP_SELF]'>
   User:<br>
   <input type='text' name='user' size='30' value='Administrator'></input><p>
   News item:<br>
   <textarea cols='70' rows='8' name='news'></textarea><p>
   <input type='submit' name='submit' value='Add News' /></form>");
   exit;
} else {
$user = $_POST['user'];
$news = $_POST['news'];
$errors = array(); //array for errors
        if (empty($user)) { //if host is empty
            $errors[] = "You Must Enter A User Name!"; //lol
        }
        if (empty($news)) { //user check
            $errors[] = "You Must Enter A News Article!"; //you fail o_0//
        }
        if (count($errors) > 0) { //if theres more then 0 errors
            print "<h2>Errors Found!</h2>
            <p>";
            foreach ($errors as $err) { //foreach error
                print $err . '';
         }
            print "</p>";
            exit();
      }
      
            if (!$cerror && !$derror) {
            if (file_exists('shownews.php') && (filesize('shownews.php') != 0)) {
               $conf_handle = fopen('shownews.php', 'r+');
               fseek($conf_handle, -8, SEEK_END);
               $data = "<tr><td>Posted by $user </td><td width='80%'> $news </td></tr>\n</table>";
               echo $content;
            } else {
               $conf_handle = fopen('shownews.php', 'w');
               $data = "<table width='100%'>\n<tr><td>Posted by $user </td><td width='80%'> $news </td></tr>\n</table>";
            }
            
                  
                   $write = fwrite($conf_handle, $data);
                   if ($write) {
                       print "<h2>Success</h2>
                           <p>
                           Your news was added.
                           </p>";
               }
         }
}
?>


That should work. It at least worked on my pc.

He forgot some closing }, the user input field had administrator after it instead of as a value and he didn't use isset in the first if statement (although I don't think that does really make a difference).

edit: wait a couple of sec with copying this, I have some other improvements.

done Smile
The shownews.php will look a bit better now. Make sure it is empty or doesn't exist before starting the script for the first time(best would be empty and chmod777).
DjMilez
Actually, I forgot that myself. THIS was an installer script, but I edited it to... that....

And thanks rvec! Very Happy


Edit: Wtf? It works when I test it on my computer at localhost... but not on my other host.
Hmm.... I'll test it out somewhere else.

Edit 2: It must be byethosts fault.
But now, whenever I post there is a new thing that prints "nnn"

Edit 3: Nevermind, I fixed it.
rvec
those nnn are newlines, I thought that would work everywhere :S
Related topics
News in PHP
PHP - BBcode Parsing
Want a news script which doesnt use MySQL?
News Script
Edit / Delete
BB Insert
php news script i arýyorum
Database Backup
Click Counter
siot.frih.net
News Script
Extreme Coders - Programming and tech support forums.
Log in system
Another problem with posting
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.