i want to post some data to my web , i am entering data to a textarea in my page and pressing submit and it will inserted in to my database

the data i typed is follows

1)Adobe Dreamweaver
2)Microsoft


but When i tried to display that it is showing like this
1)Adobe Dreamweaver2)Microsoft

how to display that with the same format i entered into the text area ( like the daniweb using)

Recommended Answers

All 14 Replies

If you would like to show the stored data with the enters that were made you need to do the following:

Assuming that there are atleast 2 columns in the table called "creator" and "message":

<?php
$q = "SELECT * FROM messagetable";
$r = mysql_query($q);
while ($row = mysql_fetch_array($r)) {
echo "
<pre>
<b>".$row['creator']."</b>
$row['message']
</pre>
";
}
?>

I have a text area in my page
I want to be able to insert into the table this text..
---------------------------
Hello
My
name

is
Josh
-------------------------
With the line breaks.

My question is: How can I add this value to the database inserting a <br /> tag after each line break?

The reason I want to do this is later in the web app I'm going to be pulling this data from the database and I want it to be already formatted .Help me please........

Well, perhaps it sounds a bit too easy but you could also insert the <pre></pre> tags within the data you save in the database:

<?php
$message = "<pre>";
$message .= $_POST['message'];
$message .= "</pre>";
$creator = $_SESSION['creator'];
$table = "messagetabel";
$q = "INSERT INTO $table (creator,message) VALUES ('$creator','$message')";
$r = mysql_query($q);
echo "
Your message:<br />
".$message."";
?>

Well, perhaps it sounds a bit too easy but you could also insert the <pre></pre> tags within the data you save in the database:

<?php
$message = "<pre>";
$message .= $_POST['message'];
$message .= "</pre>";
$creator = $_SESSION['creator'];
$table = "messagetabel";
$q = "INSERT INTO $table (creator,message) VALUES ('$creator','$message')";
$r = mysql_query($q);
echo "
Your message:<br />
".$message."";
?>

hi friend thanks for u r help , but i am using Dreamweaver , in Dreamweaver what i have to do . Please........

Member Avatar for diafol

You don't need to worry about this as your database text should store the line breaks as invisible '\n' (which means new line). You won't see this when viewing your database value in a GUI or phpMyAdmin 'cos it's an escape character and therefore invisible (I think!).
When you want to display this text (output to HTML) use nl2br($mytext) where $mytext is the string taken from the database.
If you load the text into a textarea again, you shouldn't need the nl2br function as textarea recognizes '\n'.

You don't need to worry about this as your database text should store the line breaks as invisible '\n' (which means new line). You won't see this when viewing your database value in a GUI or phpMyAdmin 'cos it's an escape character and therefore invisible (I think!).
When you want to display this text (output to HTML) use nl2br($mytext) where $mytext is the string taken from the database.
If you load the text into a textarea again, you shouldn't need the nl2br function as textarea recognizes '\n'.

<?php require_once('Connections/mysql_Connection.php'); ?>

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO generic (txtPostTitle, txtPostContent) VALUES (%s, %s)",
                       GetSQLValueString($_POST['txtPostTitle'], "text"),
                       GetSQLValueString($_POST['txtPostContent'], "text"));

  mysql_select_db($database_mysql_Connection, $mysql_Connection);
  $Result1 = mysql_query($insertSQL, $mysql_Connection) or die(mysql_error());

  $insertGoTo = "viewpost.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_mysql_Connection, $mysql_Connection);
$query_Recordset = "SELECT txtPostTitle, txtPostContent FROM generic";
$Recordset = mysql_query($query_Recordset, $mysql_Connection) or die(mysql_error());
$row_Recordset = mysql_fetch_assoc($Recordset);
$totalRows_Recordset = mysql_num_rows($Recordset);
?>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">PostTitle:</td>
      <td><input type="text" name="txtPostTitle" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right" valign="top">PostContent:</td>
      <td><textarea name="txtPostContent" cols="50" rows="5"></textarea>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset);
?>

This is my Dreamweaver code (welcome.php) to insert values into my MYSQL Table . Here in my case txtPostTitle and txtPostContents are the fields of my table and i am trying to display the posted values in my viewpost.php page . So u plz specify where i have to make changes . please....

Member Avatar for diafol

This looks fine to me. Where is your viewpost.php code? That's what you'll need to change if anything.

Am I correct in thinking that this is not your own code? It looks terribly complicated for what should be a straightforward form handling routine (even though the handling routine is generic).

Suggestion:
You are sending data to the same page as opposed to an external page (or form handling page). Although this isn't 'wrong', it can cause problems when an user reloads/refreshes the page, as the data will be sent again. Having a separate form handling page that redirects back to the original form page should obviate this problem.

Also, it doesn't seem that your posted data is 'cleaned' before inserting into your DB. This may be naughty.


As I mentioned in my earlier post if you are putting the database string into HTML, use the nl2br() function to format it:

<?php
...(previous php code)...

$mycontent = nl2br($row_Recordset['txtPostContent']);
?>

<h2><?php echo $row_Recordset['txtPostTitle']; ?></h2>
<p><?php echo $mycontent; ?></p>

Ok listen up, what he is trying to say is that in the database the enters are already stored but invisible, so on the page where you display the text you should put this where you want the text from the textarea to be shown making the \n into <br />:

$textfromtextarea = $row['message'];
$textfromtextarea = nl2br($textfromtextarea);
echo $textfromtextarea;

EDIT: i didnt see his last post.... Sorry that i posted almost the same again ;)

thanks , its working when i am entering text via keyboard , but its not working when i am copying long texts from another sites , that time my table is automatically resizing to that text length (sometimes more than 800 px ) ......... what i have to do .

Another problem is when i am entering some code in my textarea i am not getting the text

eg:
-----
i want to insert the following text into my database
<input type="text" name="text1" value="submit">

but when i tried to display the text , i am getting a 'textbox' instead of the text. Please....help me

Problem number 1 (size):

You can set the size of the textarea with the following attributes:

cols="number"
rows="number"

A column (col) is one character (_)
A row is the amount of horizontal lines, for example:

I am superman

It has 1 row, and 13 columns.

E.g:

<textarea name="myTextArea" cols="20" rows="3">Text shown in textarea</textarea>

Problem number 2 :

First of all you define the type of input with the attribute type
Please visit the page from the w3c for more information:

http://www.w3.org/TR/html401/interact/forms.html#h-17.4.1

So if you want to make a submit button the code is:

<input type="submit" name="submitbutton" value="Submit this form" />
Member Avatar for diafol

Well it will, if you echo html from a db field, it'll show it as html, what did you expect? You can do htmlentities() on the db string if you want to display the html tags as text.

And hey, wait a minute: copying long texts from other websites: that sounds really wrong to me. Am I right in thinking that you're copying code from other sites and displaying it on your own? If so, unless you have permission, stop.

I''ve had the same issue when i made a small forum for a website. I thought of the function ereg_replace():
Maybe you can use it?

<?php
$mytext = $_POST['myTextArea'];;
$mytext = ereg_replace("\[b\]","<b>", $mytext);
$mytext = ereg_replace("\[/b\]","</b>", $mytext);
echo $mytext;
?>

How to show INPUTTED data into a textarea?

Member Avatar for diafol

Start your own thread. This is a DEAD thread from SEVEN years ago. You will also need to show any RELEVANT markup, javascript / php that you have.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.