SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 71

Thread: Line Breaks

  1. #1
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Line Breaks

    How do I make the script automatically put in <br> when you skip a line while typing in a form?

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    For ASP:
    Code:
    myVar = replace(myVar, vbNewLine, "<br>")
    For PHP:
    PHP Code:
    $myVar nl2br($myVar); 

  3. #3
    Back in Action Winged Spider's Avatar
    Join Date
    Jun 2001
    Location
    outside my mind
    Posts
    900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ironic that I was just about to start looking for this function. Thanks Vinnie!

  4. #4
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    possibility, did you mean "when it's typed in", as in javascript, or "when it's submitted and processed server side" (in which case vinnie's suggestions are what you're after) ?
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  5. #5
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Second one. So when they submit the forms it automatically puts <br> in the line breaks.

    So all I type on my form page is

    Code:
    <? $myVar = nl2br($myVar); ?>
    Making it a PHP page, of course.

  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Yep!

  7. #7
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Possibility
    So all I type on my form page is
    just to clarify: you don't put it on the page with the form, but on the page that receives the form's data (the action one).

    moving to the PHP forum.
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  8. #8
    SitePoint Addict Viral's Avatar
    Join Date
    Nov 2001
    Location
    Washington DC
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, you'll need to make sure you textbox is set to that var first...
    PHP Code:
    $myVar $_POST["textboxname"];
    $myVar nl2br($myVar); 
    --Viral
    A computer without Windows is like a chocolate cake without mustard.

  9. #9
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm doesn't seem to be working. This form I am using is entering data into a database, and the action page has a MySQL querry calling it back up.

    Code:
    <? 
    $myVar = $_POST["Array[Body]"];
    $myVar = nl2br($myVar); 
    ?>
    The name of the textarea is Array[Body]. Or should I name the whole form itself? The only field that would need the line breaks is the text area, since the rest are just select boxes.

    I'll post the source below if you need it:

    Form page:
    Code:
    <html>
    <head>
    <title></title>
    <style type="text/css">
    input { 
     background-color: #444444; 
     color: #ffffff;
     font-size: 12px; 
     border: 1px solid #000000; 
    }
    textarea { 
     background-color: #444444; 
     color: #ffffff;
     font-size: 12px; 
     border: 1px solid #000000; 
    }
    select { 
     background-color: #444444; 
     color: #ffffff; 
     border: 1px solid #000000;
    }
    
    </style>
    </head>
    
    
    <body bgcolor="#777777">
    <table width="75%" align=center border=1 bordercolor=#000000 style="border-collapse: collapse;" cellpadding=5>
    <form action="HandleForm.php" method=post>
    <tr>
    <td colspan=2>
    Submit a strategy for AoM.</td></tr>
    <tr><td colspan=2 align=left>
    <select name="Which" id="which" tabindex="5">  
             <option value="Egypt">Egyptian</option>         
             <option value="Greek">Greek</option> 
             <option value="Norse">Norse</option> 
           </select>
    </td></tr>
    <tr>
    <td>
    Submitted By:</td><td> <input type=text name="Array[Name]" size=30></td></tr>
    <tr><td>Strat Name:</td><td> <input type=text name="Array[Strat]" size=30></td></tr>
    <tr><td>Strategy: </td><td><textarea name="Array[Body]" rows=7 cols=50></text></td></tr>
    <tr><td colspan=2>
    God: <select name="Array[God]" id="rate" tabindex="5">  
             <option value="All Gods">All Gods (for one culture)</option>         
             <option value="Isis">Isis</option> 
             <option value="Ra">Ra</option> 
             <option value="Set">Set</option> 
             <option value="Hades">Hades</option> 
             <option value="Poseidon">Poseidon</option> 
             <option value="Zeus">Zeus</option> 
             <option value="Loki">Loki</option> 
             <option value="Odin">Odin</option> 
             <option value="Thor">Thor</option> 
           </select>
    </td></tr>
    <tr><td colspan=2 align=right>
    <input type=submit name="submit" value="submit!"></td></tr>
    </form>
    </table>
    
    </body>
    
    
    </html>
    Action page:
    Code:
    <? include("/home/the-barr/public_html/v3/header.php") ?>
    <? 
    $myVar = $_POST["Array[Body]"];
    $myVar = nl2br($myVar); 
    ?>
    
    
    <table width="60%" border=1 bordercolor=#000000 class="content" align=center>
    <tr><td valign=center align=center>
    <a href="egyptian.php">Egyptian</a> | <a href="greek.php">Greek</a> | <a href="norse.php">Norse</a></td></tr>
    <tr><td valign=center align=center>
    <form action="egyptian.php" method=post>
    Display: <select name="Display" id="Display">
    <option value="all">All</option>
    <option value="All Gods">All Gods</option>
    <option value="Isis">Isis</option>
    <option value="Ra">Ra</option>
    <option value="Set">Set</option>
    </select>
    <input type="submit" name="submit" value="submit">
    </form>
    </td></tr>
    </table><br>
    
    
    
    <?php
    $Display = $_POST['Display'];
    $Where =  ($Display=="all" ) ? "" : "where(Strat='$Display')";
    // Set the variables for the database access:
    $Host = "localhost";
    $User = "the-barr";
    $Password = "starwars";
    $DBName = "thebarr_content";
    $TableName = "Egypt";
    
    $Link = mysql_connect ($Host, $User, $Password);
    
    $Query = "SELECT * from $TableName $Where";
    
    $Result = mysql_db_query ($DBName, $Query, $Link);
    
    // Create a table.
    print ("<TABLE WIDTH=\"60%\" BORDER=1 BORDERCOLOR=#000000 CELLSPACING=0 CELLPADDING=4 ALIGN=CENTER  class=content>\n");
    print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
    print ("<TD ALIGN=CENTER VALIGN=TOP colspan=2>Egyptian Strategies</TD>\n");
    print ("</TR>\n");
    
    // Row God and Row Strat are switched around
    while ($Row = mysql_fetch_array ($Result)) {
     	print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
     	print ("<TD ALIGN=left VALIGN=TOP WIDTH=\"50%\"><b>$Row[God]</b> - <i>$Row[Strat]</i></TD>\n");
     	print ("<TD ALIGN=right VALIGN=TOP WIDTH=\"50%\"><i>$Row[Name]</i></TD>\n");
     	print ("</TR>\n");
     	print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
     	print ("<TD ALIGN=left VALIGN=TOP colspan=2>$Row[Body]</TD>\n");
     	print ("</TR>\n");
     	print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
     	print ("<TD ALIGN=center VALIGN=TOP colspan=2>...............</TD>\n");
     	print ("</TR>\n");
    }
    mysql_close ($Link);
    print ("</TABLE>\n");
    ?>
    
    <? include("/home/the-barr/public_html/v3/footer.php") ?>
    </body></html>

  10. #10
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just thought of something, do I want to put that in HandleForm.php instead?

    Form.php takes the data, HandleForm.php puts it in the database, strats.php displays the data...

  11. #11
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, i'm having problems.

    the textarea name is Array[Body] Do I want to use that as its name or just 'Body'?

    Also, where do I put that code? In the form page, the handle form page, or the display content page?

  12. #12
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:
    $myVar = $_POST['Array[Body]'];

    Single quotes are literal and php won't take it as an array. If that doesn't work, try renaming your field without [ ], i.e. Array_Body
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  13. #13
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A) I'd recommend against using [] in the variable name. Doing so invokes PHP's automatic parsing of POST variables into arrays -> thus having to reference the variables as $_POST['Array']['Body'] (I think) -- stick with simple names.

    B) Put that code on the handle form page, to change the POSTed content correctly

    Cheers!
    My name is Steve, and I'm a super-villian.

  14. #14
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lieut_data is correct, it would be $_POST['Array']['Body'].
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  15. #15
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wish I knew about naming things with [] before, that can come in very handy for a varied number of form elements.
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  16. #16
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Extremely handy, when used in the name="array[]" format -- fantastic if you automatically generate your forms based on a table structure.
    My name is Steve, and I'm a super-villian.

  17. #17
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right now I'm working on a project with several fields, I haven't done much yet, but I was naming them field_1, field_2 automatically, I'm glad I don't have to sort through $_POST determing which fields begin with field_, lol.
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  18. #18
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *sigh* Still not working.

    I put it in my handleform.php page.

    Here is the code:

    Code:
    <HTML>
    <HEAD>
    <TITLE>Inserting Data into a Database</TITLE>
    </HEAD>
    <BODY>
    <?php
    $myVar = $_POST['Array']['Body'];
    $myVar = nl2br($myVar); 
    /* This page receives and handles the data generated by "form.html". */
    // Trim the incoming data.
    $Array["Name"] = trim ($Array["Name"]);
    $Array["Strat"] = trim ($Array["Strat"]);
    $Array["Body"] = trim ($Array["Body"]);
    $Array["God"] = trim ($Array["God"]);
    
    
    // Set the variables for the database access:
    $Host = "localhost";
    $User = "the-barr";
    $Password = "starwars";
    $DBName = "thebarr_content";
    $TableName = "$Which";
    
    $Link = mysql_connect ($Host, $User, $Password);
    $Query = "INSERT into $TableName values ('0', '$Array[Name]', '$Array[Strat]', '$Array[God]', '$Array[Body]')";
    print ("The query is:<BR>$Query<P>\n");
    if (mysql_db_query ($DBName, $Query, $Link)) {
     	print ("The query was successfully executed!<BR>\n");
    } else {
     	print ("The query could not be executed!<BR>\n");
    } 
    mysql_close ($Link);
    ?>
    </BODY>
    </HTML>

  19. #19
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try renaming the text area to Array_Body

    Then on handleform.php use this:

    $myVar = $_POST['Array_Body'];

    If you want to look at all $_POST variables to see what's available, you can use:

    print_r($_POST);
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  20. #20
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $myVar $_POST['Array']['Body'];
    $myVar nl2br($myVar); [/color]
    /* This page receives and handles the data generated by "form.html". */
    // Trim the incoming data.
    $Array["Name"] = trim ($Array["Name"]);
    $Array["Strat"] = trim ($Array["Strat"]);
    $Array["Body"] = trim ($Array["Body"]);
    $Array["God"] = trim ($Array["God"]); 
    The problem is that you've stored the body in $myVar, then used a non-existent variable: $Array, to access the other values.

    Try:

    PHP Code:
    $myVar $_POST['Array']['Body'];
    $myVar nl2br($myVar);

    // Trim the incoming data.
    $Array["Name"] = trim ($_POST['Array']['Name']);
    $Array["Strat"] = trim ($_POST['Array']['Strat']);
    $Array["Body"] = trim ($_POST['Array']['Body']);
    $Array["God"] = trim ($_POST['Array']['God']); 
    My name is Steve, and I'm a super-villian.

  21. #21
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That didn't work. A while back I took a tutorial on creating a very simple news system, and part of it was creating line breaks. This is the code from that section:

    Code:
    <?PHP
    $line = date("m.d.y") . "|" . $HTTP_POST_VARS['name'];
    $line .= "|" . $HTTP_POST_VARS['news'];
    $line = str_replace("\r\n","<BR>",$line);
    $line .= "\r\n";
    ?>
    (It was storing data in a flat file)

    Is that something I could use?

    Code:
    $line = str_replace("\r\n","<BR>",$line);

  22. #22
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $line = str_reaplce("\r\n","<BR>",$line);

    Is almost identical (just some minor differences, but nothing at all) to

    $line = nl2br($line);

    Either one will work.
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  23. #23
    Fully Sweet Car noddy's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Western Australia
    Posts
    759
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    For ASP:
    Code:
    myVar = replace(myVar, vbNewLine, "<br>" )
    For PHP:
    PHP Code:
    $myVar nl2br($myVar); 
    yeah i didnt know about it untill 2 days ago but put it into my code and works a dream

    just use nl2br();

  24. #24
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bonk
    $line = str_reaplce("\r\n","<BR>",$line);

    Is almost identical (just some minor differences, but nothing at all) to

    $line = nl2br($line);

    Either one will work.
    Actually, the biggest difference with the above code (one which is very important), is that nl2br translates \n's into XHTML compatible <br />'s, thus to emulate nl2br, you would need to adjust the str_replace with

    PHP Code:
    $line str_repalce("\r\n",'<br />',$line); 
    My name is Steve, and I'm a super-villian.

  25. #25
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It also preserves the line break, so this is the same as nl2br:

    $line = str_replace("\n","<br />\n",$line);

    But who's counting exactly what it does
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.


Bookmarks

Posting Permissions

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