Jump to content

php form not posting into my file


PlagueInfected

Recommended Posts

For some reason, when I make a post the file text will show however when I pull up my desired page. It will not update, the page stays blank.

 

<fieldset>
<legend>Updating page on <?php echo date("l, F d, Y"); ?></legend>
  <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">

   Administrator Name: <input type="text" name="name" />
   Subject: <input type="text" name="subject" />
   Text: <input type="text" name="textarea" />

   <input type="submit" value="submit" />
   <input type="reset" value="reset" />

   </form>

</fieldset>

<?php
     if (isset($_POST['name']) && $_POST['subject'] && $_POST['textarea']) {

     $root = $_SERVER['DOCUMENT_ROOT'];
     $file_name = "$root/updates.html";
     $time = date("l, F d, Y");


     $fp = fopen($file_name, 'a');
     fwrite($fp, '<div class="name">Update by: '. $_POST['name'].'</div>');
 $fp = fopen($file_name, 'a');
 fwrite($fp, '<div class="subject">Subject: '. $_POST['subject'].'</div>');
     $fp = fopen($file_name, 'a'); 
 fwrite($fp,  '<div class="mainsubject"> '. $_POST['textarea']."</div>");;
     fclose($fp);

     print 'page updated!' and include("$file_name"); 
 }
    ?>

 

my code works however i dont know why it will not update onto the page i chose to write it on, does the code look right or am i missing something

Link to comment
Share on other sites

First of all you should use the isset() function for each POST variable in your if clause not just the first one (might lead into some mysterious behavioir, didnt test it though). Second you don't need to open the file everytime you write new info to it, one open is enough until you close the file handle. Third do not use PHP_SELF, use $_SERVER['SCRIPT_NAME'] instead since PHP_SELF is vulnerable to XSS attacks if not used properly. Fourth I think that include should be: include($file_name); Fifth I don't know if that 'and' thing works, atleast I have never seen it being used before (if it's not ok you should print on one line and include in another).

Link to comment
Share on other sites

I tried what you all suggested, I include the file...it appears when the page loads however when I go to the actual link, the post will not appear.

 

Here is how I updated the code

 

<?php
     $root = $_SERVER['DOCUMENT_ROOT'];
     $file_name = $_SERVER['DOCUMENT_ROOT'] . '/updates.html';

     $time = date("l, F d, Y");

     if (isset($_POST['name'])) {
     $fp = fopen($file_name, "a");
     fwrite($fp, '<div class=\"name">Update by: '. $_POST['name'].'</div><br />');
 }
 if (isset($_POST['subject'])){
 $fp = fopen($file_name, "a");
 fwrite($fp, '<div class=\"subject">Subject: '. $_POST['subject'].'</div><br />');
 }
 if (isset($_POST['textarea'])){
     $fp = fopen($file_name, "a");
 fwrite($fp,  '<div class=\"mainsubject"> '. $_POST['textarea']."</div><br />");
     fclose($fp);
     
     print 'page updated'; 
 }
 include($file_name); 
    ?>
    

<fieldset>
<legend>Updating page on <?php echo date("l, F d, Y"); ?></legend>
  <form action="<?php $_SERVER['SCRIPT_NAME']; ?>" method="post">

   Administrator Name: <input type="text" name="name" />
   Subject: <input type="text" name="subject" />
   Text: <input type="text" name="textarea" />

   <input type="submit" value="submit" />
   <input type="reset" value="reset" />

   </form>

</fieldset>

Link to comment
Share on other sites

Try this.. notice how the textarea is used btw, you used it in wrong way. Also notice the added 'echo' in the forms action attribute before $_SERVER['SCRIPT_NAME']. Without that echo the action of the form is blank.

 

<?php
$fileName = $_SERVER['DOCUMENT_ROOT'] . '/updates.html';
// Do a dump and see if the path to the file is correct
// (remove the line below when you don't need it anymore).
var_dump($fileName); 

$time = date("l, F d, Y");

if (isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['textarea']))
{
$fp = fopen($fileName, "a");
fwrite($fp, '<div class="name">Update by: ' . $_POST['name'] . '</div><br />');
fwrite($fp, '<div class="subject">Subject: ' . $_POST['subject'] . '</div><br />');
fwrite($fp, '<div class="mainsubject"> ' . $_POST['textarea'] . '</div><br />');
fclose($fp);

echo 'Page updated';
}
include($fileName);
?>
<fieldset>
<legend>Updating page on <?php echo date("l, F d, Y"); ?></legend>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
Administrator Name: <input type="text" name="name" />
Subject: <input type="text" name="subject" />
Text: <textarea name="textarea"></textarea>
<input type="submit" value="submit" />
<input type="reset" value="reset" />
</form>
</fieldset>

Link to comment
Share on other sites

done it, it works but when I go to the link that is on the site it does not appear and I need the include of the filename to appear to visitors for current events and updates.

 

here is the direct link it is going to http://plagueinfected.com/admin/updates.html

 

and i used the same code you gave me, it works but it doesnt post onto the html page

Link to comment
Share on other sites

Have you manually checked the html file contents after posting data to it? Is the data there as it should be? I looked the source and the source is blank. Btw do you have error display on? So you see if there is errors in your code.

 

If not, put this in the beginning of your php file(s).

error_reporting(E_ALL);
ini_set('display_errors', 1);

Link to comment
Share on other sites

Try:

<?php
ini_set('error_reporting',E_ALL); // error reporting on
$fileName = $_SERVER['DOCUMENT_ROOT'] . '/updates.html';
// Do a dump and see if the path to the file is correct
// (remove the line below when you don't need it anymore).
var_dump($fileName); 

$time = date("l, F d, Y");
//check to see if posts are set or not empty
if (isset($_POST['name'])|| isset($_POST['subject'])|| isset($_POST['textarea']) ||!empty($_POST['textarea'])||!empty($_POST['subject'])||!empty($_POST['name']))
{

$divname='<div class="name">Update by: ' . $_POST['name'] . '</div><br />';
$divsubject='<div class="subject">Subject: ' . $_POST['subject'] . '</div><br />';
$divtextarea='<div class="mainsubject"> ' . $_POST['textarea'] . '</div><br />';
   $fp = fopen($fileName, "r+"); // open socket for read and write
   fwrite($fp,$divname);
   fwrite($fp,$divsubject);
   fwrite($fp,$divtextarea);
   fclose($fp);
   
   echo 'Page updated';
}
include($fileName);
?>
<fieldset>
   <legend>Updating page on <?php echo date("l, F d, Y"); ?></legend>
   <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
   Administrator Name: <input type="text" name="name" />
   Subject: <input type="text" name="subject" />
   Text: <textarea name="textarea"></textarea>
   <input type="submit" value="submit" />
   <input type="reset" value="reset" />
   </form>
</fieldset>

Link to comment
Share on other sites

now what do you get?

 

<?php
ini_set('error_reporting',E_ALL); // error reporting on
$fileName = $_SERVER['DOCUMENT_ROOT'] . '/updates.html';
// Do a dump and see if the path to the file is correct
// (remove the line below when you don't need it anymore).
var_dump($fileName); 

$time = date("l, F d, Y");
//check to see if posts are set or not empty
if (isset($_POST['name'])|| isset($_POST['subject'])|| isset($_POST['textarea']) ||!empty($_POST['textarea'])||!empty($_POST['subject'])||!empty($_POST['name']))
{

$divname='<div class="name">Update by: ' . $_POST['name'] . '</div><br />';
$divsubject='<div class="subject">Subject: ' . $_POST['subject'] . '</div><br />';
$divtextarea='<div class="mainsubject"> ' . $_POST['textarea'] . '</div><br />';
//check to see if file is even writtable

if (is_writable($fileName)) {
if (!$handle = fopen($fileName, 'r+')) {
         echo "Cannot open file ($filenName)";
         exit;
    }
   $fp = fopen($fileName, "r+"); // open socket for read and write
     // Write $somecontent to our opened file.
    if (fwrite($fp, $divname) === FALSE) {
        echo "Cannot write to file ($fileName)";
        exit;
    }
    
    elseif(fwrite($fp, $divsubject) === FALSE) {
        echo "Cannot write to file ($fileName)";
    exit;}
       elseif(fwrite($fp, $divtextarea) === FALSE) {
        echo "Cannot write to file ($fileName)";
    exit;}
   fwrite($fp,$divname);
   fwrite($fp,$divsubject);
   fwrite($fp,$divtextarea);
   fclose($fp);
   
   echo 'Page updated';
}
} else{
  echo "The file $fileName is not writable";
}
include($fileName);
?>
<fieldset>
   <legend>Updating page on <?php echo date("l, F d, Y"); ?></legend>
   <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
   Administrator Name: <input type="text" name="name" />
   Subject: <input type="text" name="subject" />
   Text: <textarea name="textarea"></textarea>
   <input type="submit" value="submit" />
   <input type="reset" value="reset" />
   </form>
</fieldset>

Link to comment
Share on other sites

is it outputting any error? if so which one? we need to know what it is outputting if anything in ordder to determine why it isnt writing. :P

 

something i noticed but your path is wrong.

 

<?php
// making sure the file is in the admin directory
$base='http://plagueinfected.com/';
$filename = $base'admin/updates.html';?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.