PHP Programming > Beginners - Learning PHP

add and edit text content

(1/2) > >>

HSboy:
Hello all,
I am beginner in php and I have a few quenstions for my current project.  I am working on basic php system that an editor add and edit text content (the website is designed for articles ).

add.php
<form action="add.php" method="post" name="form1">
  <table width="25%" border="0">
    <tr>
      <td>Title</td>
      <td><input type="text" name="title"></td>
    </tr>
    <tr>
      <td>Description</td>
      <td><input type="text" name="descrip">
   </tr>
    <tr>
      <td>Text</td>
      <td><input type="text" name="text" id="edit_tx"></td>
    </td>
    </tr>
    <tr>
      <td></td>
      <td><input type="submit" name="Submit" value="Add" id="submit"></td>
    </tr>
  </table>
</form>

--- PHP Code: ---
{
//insert data to database
$result=mysql_query("INSERT INTO content(title,descrip,text) VALUES('$title','$descrip','$text')");
header("Location:member-index.php");
}

--- End code ---


edit.php
<form name="form1" method="post" action="edit.php">
<table border="0">
  <tr>
    <td>Title</td>
    <td><input width="212" type="text" name="title" value=<?php echo $title;?>>     </td>
  </tr>
  <tr>
    <td>Description</td>
    <td><input type="text" name="descrip" value=<?php echo $descrip;?>>     </td>
  </tr>
  <tr>
    <td>Text</td>
    <td><input type="text" name="text" value=<?php echo $text;?> id="edit_tx">     </td>
  </tr>
 <tr>
    <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>>    </td>
    <td><input type="submit" name="update" value="Update" id="submit"></td>
  </tr>
</table>
</form>

--- PHP Code: ---
$id = $_GET['id'];
//selecting data associated with this particular id
$result=mysql_query("select * from content where id=$id");

while($res=mysql_fetch_array($result))
{
$title = $res['title'];
$descrip = $res['descrip'];
$text = $res['text'];
}

--- End code ---


index.php

--- PHP Code: ---
echo "<div id='site_content'>";

while($res=mysql_fetch_array($result)){

echo "<hr><h2>".$res['title']."</h2>";
echo "<h3>".$res['descrip']."</h3>";
echo "<pre>".$res['text']."</pre>";
echo "<a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\">Delete</a><hr>";
}
echo "</div>";


--- End code ---

My question is: Is it correct to use only <input> tag for the form or I can use <textarea> tag for adding and editing the content? Because when is <input> - it doesn not show the whole content, and if I use <textarea> - when I edit it doen't show nothing.

My other question is: How can I format the the data (the text) in the index.php? I tryed to format the text with css, but always the text is showed in one line.

Thanks in advance!

wilson382:
Hello,

for large amount of data is better to use textarea, you use it the same way as input type="text"

to format the data when you retrieve from the database you can use
--- PHP Code: ---nl2br($rows['Examples'])
--- End code ---


this will retrieve it the same way you enter it

ErnieAlex:
Well, Wilson is correct.  If you have a large amount of data, you should use TextArea tags.  But, when converting the values of them, you have to convert tags to database format if you want to store them inside of MySQL databases or others.  I have found after months of experiments that there is a very nice solution to this process.  I use an add-in called TinyMCE.  It is a simple to add in and simple to use plug in.  Basically, download it, copy it to your site and make a few small configuration options.  You can add all kinds of options such as BOLD, ITALICS, and a ton of others...  Oh, you are using a similar version as we speak...  Look above at the options you can do in this text-area!!!

So, here is a link to cover it.  By the way, it automatically saves to a MySQL database and converts all of the needed tags for you without any extra code.  So, WHAT-YOU-SEE-IS-WHAT-YOU-GET...  Try it out!!!
   http://www.tinymce.com/tryit/full.php
(Go to the download area!)
   AND, it is totally FREE...  I have customized it in a TON of ways... Real nice for free!

HSboy:
It is work when the the values are out of the <textarea> tag for the form:


--- Code: ---   <textarea type="text" name="text" value=<?php echo $text;?>><?php echo $text;?></textarea>

--- End code ---

but for <p> tag I used

--- PHP Code: ---
   echo "<p>".nl2br($res['text'])."</p>";

--- End code ---


Thanks :)

ErnieAlex:
Perhaps I did not understand your question...   Your textarea code is incorrect.
You had:

--- PHP Code: ---
   <textarea type="text" name="text" value=<?php echo $text;?>><?php echo $text;?></textarea>

--- End code ---

It should be more like this:

--- PHP Code: ---
   <textarea type="text" name="text"><?php echo $text;?></textarea>

--- End code ---

You do not set a value in textarea, just place the text inside the tags.  You still can read it using the textarea's "name" field which returns the text between the tags.

Navigation

[0] Message Index

[#] Next page

Go to full version