...

View Full Version : Textarea content to save in database



lovesirius12
12-12-2006, 05:22 AM
i have this site which is like a blog. but whatever i enter into the textarea, even with a lot of enter key, it still end up as one single line when i save it in the database. could you help me how to solve that? thanks.

lovesirius12
12-12-2006, 06:31 AM
it's okay now. i already figured it out. just add <pre> tag on the text before saving it to the database.

Shadowfox.web
12-12-2006, 07:15 PM
teach me how to do that!! PLEASE!!! i've been trying to figure out a code to make a blog, but i'm having no luck. i want to be able to post a blog and have people be able to post a comment.

whizard
12-12-2006, 07:25 PM
Lovesirius:

You can use the 'WRAP' attribute with the <textarea> tag. There are three options:

<textarea wrap="virtual" />
This means that the text in the box wraps, but it is sent as one long continuous string.

<textarea wrap="physical" />
This means that the text in the box wraps, and it is sent that way too.

<textarea wrap="off" />
This means that the text in the box does not wrap, but is sent exactly the way it was typed in.


Shadowfox:

The best way to do this is to use a server side language, such as PHP or ASP. What you would have to do is setup a form for the person to enter their comments and other relevant info (name, email, etc). Then the form would send the info to a script which processes it and stores it in a database or somewhere else. Then on your blog page, you would use a script to retrieve the data from the database and write it to the page.

Dan

Shadowfox.web
12-12-2006, 07:38 PM
Shadowfox:

The best way to do this is to use a server side language, such as PHP or ASP. What you would have to do is setup a form for the person to enter their comments and other relevant info (name, email, etc). Then the form would send the info to a script which processes it and stores it in a database or somewhere else. Then on your blog page, you would use a script to retrieve the data from the database and write it to the page.

Dan
um hmm.... i've never used any source code other than html and css and a few java source codes that i copied from a generator. What would be the best? PHP? ASP? Or mySQL?<someone told me to use that on another forum

whizard
12-12-2006, 07:44 PM
By Java, I assume you mean JavaScript? Java ans JavaScript are different languages...

Anyhow, I would personally recommend PHP & MySQL.

However, your choice will depend more on what your server supports.

You use the PHP (or other language) to process the input from the form and then you use the MySQL to insert into the database.

I'm willing to explain more in depth if that would be helpful!

Dan

Shadowfox.web
12-12-2006, 07:57 PM
By Java, I assume you mean JavaScript? Java ans JavaScript are different languages...

Anyhow, I would personally recommend PHP & MySQL.

However, your choice will depend more on what your server supports.

You use the PHP (or other language) to process the input from the form and then you use the MySQL to insert into the database.

I'm willing to explain more in depth if that would be helpful!

Dan
well i did take a java programming class but i do mean javascript and yes explaining in depth would be helpful. that is if you dont mind?

Shadowfox.web
12-12-2006, 08:10 PM
either that or send me a link to a site that expalins it. i'll try googling php tutorials, i'll get back to you if i find a good one in about ten minutes, ok

whizard
12-12-2006, 08:30 PM
Not at all!

Say you have this form:



<form name="blog_comments" action="PATH TO PHP PARSER" method="post">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="submit" name="submit" value="Submit" />
<textarea name="comment" />
</form>


you would have a database with four fields (for simplicity, you could have more):
ID, NAME, EMAIL, COMMENT

So you have to get the information from the form to the database.

This is where the php comes in. In the <form> tag above, there was an attribute called "action". This tells the form where to send the information. You would need to create a PHP file to process the info from the form, and point the form at it, by setting <form action=""> to the path of your PHP file.

Then the form will send the information to the PHP file when the user presses submit.

The PHP file will recieve each text field's data and put it into a variable like this: $_POST['NAME'];, where 'NAME' is the value of the name attribute of each input.

In the PHP file, you could run validations on each of the inputs - making sure that the email address is a valid address, stripping or converting HTML tags so that the user can't inject harmful code into your database, and things like that. Once you have the data validated, you use MySQL to put it into the Database.



$query = "INSERT INTO `tablename` (name,email,comment) VALUES ('$name', '$email', '$comment')";




So, in all, the code would look something like this:

form_parser.php


//Open connection to MySQL and correct database
$connect = mysql_connect(MySQL server,MySQL username,MySQL password) or die("Could not connect to MySQL");
$db = mysql_select_db(database name,$connect) or die("Could not connect to database \"<strong>database name</strong>\"");

//get the data from the form (could just use the $_POST['xxx']; variable, but that gets unwieldy...
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

//Run validations...
//Look around on this site, ask questions, do searches to find out how to validate, it's too involved for this example, but don't skip it, it's important

//Create query to insert into DB
$query = "INSERT INTO `tablename` (name,email,comment) VALUES ('$name', '$email', '$comment')";
//Run Query
if(! mysql_query("$query"))
{ //Show error msg...
include error.php;
}
else
{
//Show success message
header(Location:form_success.php);
}


and the code to retrieve:


//Remeber to do all the connection stuff from above
// Create query
$query = "SELECT * FROM `tablename`";
//Run Query
$result = mysql_query($query,$connect) or die(mysql_error());
while($data = mysql_fetch_array( $result ))
{
//This is a loop that will repeat for each record in database
//You can certainly include html and design elements in this process...
print $data['name'];
print "<br />";
print $data['email'];
print "<br>";
print $data['comment'];
}


That should do it!

HTH, Dan

Shadowfox.web
12-12-2006, 08:44 PM
So would i put the input code and then the code to retrieve in seperate php docs?

whizard
12-12-2006, 08:53 PM
Yes.

On your home page, where you have the blog, when you got to the part where you want the comments to show up, you would put the retrieval code in (either type it directly in, or use the include command).

Then, you would probably have the form on the same page as well, below the comments. (just my guess)

The form would point to the insertion code file, which would insert the code and then redirect the user to the main page again (or whereever you want them to go) maybe with a message that they had successfully added a comment

Dan

Shadowfox.web
12-12-2006, 09:22 PM
the form is on the same page, im using frames though so i might put it into a different frame or just leave it in that doc

whizard
12-12-2006, 09:27 PM
Either way would work.

Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum