Jump to content

[SOLVED] make forms keep data after being posted


scarhand

Recommended Posts

I'm trying to set up a simple preview-type thing for my very first blog script (im new to php and mysql).

 

First off, heres my code for the update (place to post a new article) page (excluding the includes to connect to the database):

 

function postNews() {
  global $db;

  $title = $_POST['title'];
  $newstext = $_POST['newstext'];
    
  $query = "INSERT INTO news (title, newstext) VALUES ('$title', '$newstext')";
  mysql_query($query) or die('Error, MySQL query failed');
  
  echo "<div class=\"evenbg\" style=\"padding: 10px;\"> \n";
  echo "Article <b>'$title'</b> has been added.<br> \n";
  echo "Go to the <b><a href=\"index.php\">index</a></b> to check it out.<br> \n";
  echo "</div> \n \n";
}

function postPreview() {

  $title = $_POST['title'];
  $newstext = $_POST['newstext'];
  
  echo "<div class=\"oddbg\"> \n";
  echo "<table width=\"100%\" cellpadding=\"10\" cellspacing=\"0\" border=\"0\"> \n";
  echo "<tr> \n";
  echo "<td><font class=\"newstitle\">$title</font></td> \n";
  echo "</tr> \n";
  echo "</table> \n";
  echo "</div> \n";
    
  echo "<div class=\"evenbg\"> \n";
  echo "<table width=\"100%\" cellpadding=\"10\" cellspacing=\"0\" border=\"0\"> \n";
  echo "<tr> \n";
  echo "<td colspan=\"2\" class=\"newsbox\">$newstext</td> \n";
  echo "</tr> \n";
  echo "</table> \n";
  echo "</div> \n";
}

if (isset($_POST['save']))
{

  if (empty($_POST['title']))
{
    echo "<div class=\"errordiv\"><b>Error:</b> You must enter a title for the new article.</div>";
  }
  elseif (empty($_POST['newstext']))
  {
    echo "<div class=\"errordiv\"><b>Error:</b> You must enter some content for the new article.</div>";
  }
  else
  {
    postNews();
  }

}
elseif (isset($_POST['preview']))
{

  if (empty($_POST['title']))
{
    echo "<div class=\"errordiv\"><b>Error:</b> You must enter a title for the new article.</div>";
  }
  elseif (empty($_POST['newstext']))
  {
    echo "<div class=\"errordiv\"><b>Error:</b> You must enter some content for the new article.</div>";
  }
  else
  {
    postPreview();
  }

}

echo "<form method=\"post\">";
echo "<div class=\"oddbg\">";
echo "<table width=\"100%\" cellpadding=\"10\" cellspacing=\"0\" align=\"center\" border=\"0\">";
echo "  <tr>";
echo "    <td width=\"70\" align=\"right\"><b>Title:</b></td>";
echo "    <td>";
echo "    <input name=\"title\" type=\"text\" id=\"title\" maxlength=\"50\" style=\"width: 100%\">";
echo "    </td>";
echo "  </tr>";
echo "</table>";
echo "</div>";

echo "<div class=\"evenbg\">";
echo "<table width=\"100%\" cellpadding=\"10\" cellspacing=\"0\" align=\"center\" border=\"0\">";
echo "  <tr>";
echo "    <td width=\"70\" valign=\"top\" align=\"right\"><b>Content:</b></td>";
echo "    <td>";
echo "    <textarea name=\"newstext\" rows=\"18\" id=\"newstext\" style=\"width: 100%\">";
echo "    </textarea>";
echo "    </td>";
echo "  </tr>";
echo "</table>";
echo "</div>";
echo "<br>";
echo "<input name=\"save\" type=\"submit\" id=\"save\" value=\"Add the Article\">";
echo "   ";
echo "<input name=\"preview\" type=\"submit\" id=\"preview\" value=\"Preview the Article\">";
echo "</form>";

 

Now I have tried making the value for the textarea to be this:

 

value="<?php $newstext ?>"

 

But it still gets removed after being posted.

 

Any help would be greatly appreciated as I've been searching google for a solution for the past hour.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.