Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Aug 2005
    Location
    Laguna, Philippines
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Textarea content to save in database

    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.
    "It is the unknown we fear when we look upon death and darkness, nothing more!"-- Albus Dumbledore

  • #2
    New Coder
    Join Date
    Aug 2005
    Location
    Laguna, Philippines
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's okay now. i already figured it out. just add <pre> tag on the text before saving it to the database.
    "It is the unknown we fear when we look upon death and darkness, nothing more!"-- Albus Dumbledore

  • #3
    New Coder
    Join Date
    Dec 2006
    Location
    Sandy, UT
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    New Coder
    Join Date
    Dec 2006
    Location
    Sandy, UT
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    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

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #7
    New Coder
    Join Date
    Dec 2006
    Location
    Sandy, UT
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    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?

  • #8
    New Coder
    Join Date
    Dec 2006
    Location
    Sandy, UT
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Not at all!

    Say you have this form:

    Code:
    <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.

    PHP Code:
    $query "INSERT INTO `tablename` (name,email,comment) VALUES ('$name', '$email', '$comment')"


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

    form_parser.php
    PHP Code:
    //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:
    PHP Code:
    //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
    Last edited by whizard; 12-12-2006 at 08:33 PM. Reason: [B] doesn't work inside PHP code tags.. oops..
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #10
    New Coder
    Join Date
    Dec 2006
    Location
    Sandy, UT
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So would i put the input code and then the code to retrieve in seperate php docs?

  • #11
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #12
    New Coder
    Join Date
    Dec 2006
    Location
    Sandy, UT
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Either way would work.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •