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 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts

    <br> in a text area

    How can I include the <br> whenever someone writes something in a textarea field and has many <br> in it?
    I did it and its showing it as a 1 line!

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    \n is a newline in plaintext

    $str = "blah \n blah \n blah";

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Yes. but I'm using it as variables.

    I have 2 files, index.php and insert_db.php and this is the code of each

    index.php
    PHP Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Thoughts</title>
    </head>
    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

    <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="527" bgcolor="#CCCCCC">&nbsp
          <form action="insert_db.php" method="POST">
      <label>
      <div align="center">
        <p>
          <textarea name="quote" cols="50" rows="20"></textarea>
          <br />
          <br />
          name:
          <input type="text" name="name" />
          <br />
          <br />
          email:
          <input type="text" name="email" />
          <br />
          </p>
        </div>
      </label>
      <div align="center">
        <input type="submit" value="Submit" />
      </div>
    </form>;</td>
        <td width="500" bgcolor="#666666"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td valign="top"><div align="center"><font color="#FFFFFF"><h1>Latest Submissions</h1></font></div></td>
            </tr>
            <tr>
              <td valign="middle"><div align="center">
                  <?php
      $mysqli 
    mysqli_connect("localhost""root""""blackquote");
      if (
    mysqli_connect_errno()) {
          
    printf("Connect failed: %s\n"mysqli_connect_error());
          exit();
      } else {
          
    $sql "SELECT * FROM data ORDER BY id desc LIMIT 5";
          
    $res mysqli_query($mysqli$sql);
          if (
    $res) {
              while (
    $newArray mysqli_fetch_array($resMYSQLI_ASSOC)) {
                  
    $id  $newArray['id'];
                  
    $quote $newArray['quote'];
                  
    $email $newArray['email'];
         
    $name $newArray['name']; ?>
                    <?php echo"".$quote."" ?> - <i>By <a href="mailto:<?php echo "".$email."" ?>"><?php echo "".$name."" ?></a></i><br />
                    Quote #<?php echo "".$id."" ?><br />
                    <br />
                    <?php
                
    //  echo"".$quote." - <i>By ".$name."</i><br />Thought #".$id."<br/><br />";
                 
    }
          } else {
              
    printf("Could not retrieve records: %s\n"mysqli_error($mysqli));
          }
          
    mysqli_free_result($res);
          
    mysqli_close($mysqli);
      }
    ?>
              </div></td>
            </tr>
            <tr>
              <td valign="bottom"><div align="center">
      <?php 
              $link 
    mysql_connect("localhost""root""");
              
    mysql_select_db("blackquote"$link);
    $result mysql_query("SELECT * FROM data"$link);
    $num_rows mysql_num_rows($result);
    ?> 
      <div align="center">
      <span class="style3">Now Serving</span> 
      <INPUT 
    style="FONT-WEIGHT: bold; FONT-SIZE: 8pt; FONT-FAMILY: verdana; TEXT-ALIGN: center" 
    onfocus=this.blur() size=11 value="<?php echo "$num_rows"?>">
              </div></td>
            </tr>
        </table></td>
      </tr>
    </table>
    </body>
    </html>
    insert_db.php

    PHP Code:
    <?php 
    $con 
    mysql_connect("localhost","root"); 
    if (!
    $con
      { 
      die(
    'Could not connect: ' mysql_error()); 
      }
    mysql_select_db("blackquote"$con);
      
    $sql="INSERT INTO data 
    (quote,name,email) 
    VALUES 
    ('$_POST[quote]','$_POST[name]','$_POST[email]')"
    ;if (!mysql_query($sql,$con)) 
      { 
      die(
    'Error: ' mysql_error()); 
      }
      
    header("location: redirect.php");
    exit(); 
    ?>
    as you can see, i'm not using a plain text, but im fetching records from the database, how can I include the "\" in it?

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Anything in a textarea is plaintext, which is why you can type html in there and it'll show as text.

    First thing, change these to have quotes

    PHP Code:
    VALUES 
    ('".$_POST['quote']."','".$_POST['name']."','".$_POST['email']."'
    So you have no line breaks in your code at all and have <br>'s only?

    use
    PHP Code:
    $str str_replace("<br>","\n",$str); 
    That'll replace the <br>'s to newlines in the textarea

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Where would I use it?

  • #6
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I don't think it's working coz I added it into insert_db.php

    PHP Code:
    <?php
    // -- add the name values of other fields you want here 
        
    $required_fields = array('quote','name','email'); 
        foreach(
    $_POST as $key => $value
        { 
             if (
    in_array($key,$required_fields) && strlen(trim($value)) == 0
            { 
                
    // -- required field was not filled in! 
                
    $errors[] = 'You need to fill in '.$key.'!'
            } 
        } 
        if (
    is_array($errors)) 
        { 
            
    // -- there were some errors, lets print them out 
            
    echo 'There were some errors with your form : <br />'
            echo 
    implode('<br />',$errors); 
            echo 
    'Please correct them before continuing!'
        } else { 
            
    // -- continue with the rest 
        
    }
    $con mysql_connect("localhost","root");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
    mysql_select_db("blackquote"$con);
      
    $str str_replace("<br>","\n",$str); 
      
    $quote=mysql_real_escape_string($_POST['quote']);
      
    $name=mysql_real_escape_string($_POST['name']);
      
    $email=mysql_real_escape_string($_POST['email']);
    $sql="INSERT INTO data (quote,name,email) VALUES ('$quote','$name','$email')";
    if (!
    mysql_query($sql,$con))
      {
      die(
    'Error: ' mysql_error());
      }
    header("location: redirect.php");

    //   } 
    ?>
    I've added the upper code to make all my fields required but if I leave an empty field it'll tell me that I need to fill in all the fields but it'll add an empty record to the database, how can I fix that?

    Thank you for your help man.
    Last edited by Snitz; 03-29-2007 at 11:11 AM.

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Actually, I think I misread it. Are you wanting <br>'s to replace a newline in a textarea, or <br>'s to be replaced by newlines?

    If it's the first one, then use nl2br().

    You use it on whatever variable it is that you're wanting it used on.

  • #8
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    It's just not working, can you look at the code above and guide me please?
    Thank you!

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Tell me which way you're wanting it first. Are you wanting <br>'s to show or newlines to show?

  • #10
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I have a text area and for example if I wanna write a text with new lines in it, when I fetch it I want them to have break lines: for example:

    while entering my data in textarea:
    line1
    line2
    line3

    when I fetch them, I don't want them to look like this:
    line1 line2 line3, I want them to look just like I posted them!

  • #11
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Ok..
    PHP Code:
    $sql="INSERT INTO data (quote,name,email) VALUES ('nl2br($quote)','$name','$email')"

  • #12
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    Ok..
    PHP Code:
    $sql="INSERT INTO data (quote,name,email) VALUES ('nl2br($quote)','$name','$email')"
    That didn't work
    it gave me the following
    nl2br(this is whaty I want) - By snitz
    Quote #26

  • #13
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Sorry, I'm still half asleep.
    PHP Code:
    $quote=mysql_real_escape_string(nl2br($_POST['quote']));
      
    $name=mysql_real_escape_string($_POST['name']);
      
    $email=mysql_real_escape_string($_POST['email']);
    $sql="INSERT INTO data (quote,name,email) VALUES ('$quote','$name','$email')"
    That should work

  • #14
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    Sorry, I'm still half asleep.
    PHP Code:
    $quote=mysql_real_escape_string(nl2br($_POST['quote']));
      
    $name=mysql_real_escape_string($_POST['name']);
      
    $email=mysql_real_escape_string($_POST['email']);
    $sql="INSERT INTO data (quote,name,email) VALUES ('$quote','$name','$email')"
    That should work
    Finally it worked.
    can I ask other questions or are you sick of me now? :$

  • #15
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Can somebody help create a small search page out of these records?


  •  

    Posting Permissions

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