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.

Link to comment
Share on other sites

try session's ok

 

<?php session_start();// << must be set to top off page ok.

 

$_SESSION['news']=$news;

$_SESSION['titke=$title;

 

?>

 

value=<?echo $_SESSION['news'];?>

value=<?echo $_SESSION['title'];?>

 

did the trick, thanks!

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.