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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Line Break Issue

    Hey guys. I'm working on a "Preview Post" function. When the Preview Post button is clicked, it should reload the page, show the preview above the textarea, then send the text back to the textarea so the user can keep working.

    It works until I move to a second line. If there are line breaks they don't get shown in the preview. Also, the text isn't sent back to the textarea.

    I have a pair of functions that are supposed to handle the input string. The first function records the input when typing, and the second function picks up the post to print as a preview: You can see what I mean here:

    Code:
    function preview() 
    {
      var preview = window.document.postform.inputpost.value;
      window.document.hidden.inputpreview.value = preview;
    }
    
    function copypreview()
    {
      var copypreview = '<?php echo $_POST[inputpreview]; ?>';
      window.document.postform.inputpost.value = copypreview;
    }
    Here's how I use them:

    Code:
    <body onload='copypreview();'>
    
    <?php include("navbar.php"); ?> 
    
    <?php if ($_GET[preview])
    {
      echo "<table id=blog><tr><td>";
      echo $_POST[inputpreview];
      echo "</td></tr></table>";
    } ?>
    
    <form name="postform" action="submitpost.php" method="post">
    
    Post Title 
    <input size="40" type="text" name="inputtitle" id="inputtitle">
    
    <?php include("tagbar.php"); ?>
    
    <textarea onchange="preview();" cols="101" rows="20" id="inputpost" name="inputpost"></textarea>
    
    <div align=center>
    <input type="submit" value="Submit"/>
    </form>
    
    <form name="hidden" method="post" action="addpost.php?preview=1">
    <input type="hidden" name="inputpreview">
    <input type="submit" onClick="preview();" value="Preview Post"/>
    </form>
    Sorry I know it's a lot of code. I tried to make it as readable as possible. Can someone tell me how to fix this?

    I thought I might have to find a way to insert <br> tags when the user hits the Enter key, but I could have sworn this thing was working with line breaks before.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Put the text between <pre>...</pre> tags.

    Or do a replace: replace each newline with <br/> followed by a newline, for example.

    Line breaks are converted to spaces by HTML. And multiple spaces are converted to a single space.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the reply. I have html tags that are inserted into the text for formatting purposes. Will <pre> cause those tags to be ignored?

    How would I go about detecting a newline? I'm fairly new to javascript but I'm sure I could figure it out if you point me in the right direction.

  • #4
    New Coder
    Join Date
    Aug 2010
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There is an article where you can also find some examples.

  • #5
    New Coder
    Join Date
    Aug 2010
    Location
    FL
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post
    Ntwiles:

    How would I go about detecting a newline? I'm fairly new to javascript but I'm sure I could figure it out if you point me in the right direction.
    Detecting the new lines in PHP would be the easiest because you will be dealing with the posted data anyways..

    Do something like:

    Code:
    $content = str_replace("\n", "<br/>\n", $content);
    Be sure to use the double quotes or PHP will not replace your string correctly because it interprets the newline literally.

  • #6
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks so much for the help guys. I've spent a lot of time reworking my entire code and I've manage to replace the javascript preview() with this:

    PHP Code:
    <?php 
    $preview 
    $_POST['inputpost'];
    $preview str_replace("\n""<br/>"$preview);
    echo 
    "<table id=blog><tr><td>";
    echo 
    $preview;
    echo 
    "</td></tr></table>";
    ?>
    But I can't do the same thing with the copypreview() function. So honestly I'm in the same boat as when I started the thread, just in a less convoluted way.

    PHP Code:
    function copypreview()
    {
      <?php 
      $copypreview 
    $_POST['inputpost'];
      
    $copypreview str_replace("\n""<br/>\n"$copypreview); 
      
    ?>
      var copypreview = '<?php echo $copypreview?>';
      window.document.postform.inputpost.value = copypreview;
    }
    My problem is that I need to use PHP to recieve the post data, but I need to use javascript to send it to the textarea. I can't for the life of me find a way to make the two work together. I thought I had it here, but apparently printing the string to set the value as a javascript variable instead of a PHP variable ruins the whole thing and causes some really weird format errors. So I guess what I should have asked from the beginning is can someone show me a roundabout way to convert a PHP string to a Javascript string?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    OH! Is *THAT* what you meant!!!

    Okay:

    Code:
    function copypreview()
    {
      <?php 
      $copypreview = $_POST['inputpost'];
      $copypreview = str_replace("\n", "\\n", $copypreview); 
      ?>
      var copypreview = '<?php echo $copypreview; ?>';
      window.document.postform.inputpost.value = copypreview;
    }
    But watch out, there could be other "poison" characters. Might need to replace \r with \\r, for example. And *MUST* replace ' with \'. Let's fix up " too, just in case.

    Maybe:
    Code:
    function copypreview()
    {
      <?php 
      $copypreview = $_POST['inputpost'];
      $copypreview = str_replace("\n", "\\n", $copypreview); 
      $copypreview = str_replace("\r", "\\r", $copypreview); 
      $copypreview = str_replace("'", "\\'", $copypreview); 
      $copypreview = str_replace("\"", "\\"", $copypreview); 
      ?>
      var copypreview = '<?php echo $copypreview; ?>';
      window.document.postform.inputpost.value = copypreview;
    }
    Might find other characters, but should clearly fix those.

    We have to put in \\ so that PHP will put a single \ into the string.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Ntwiles (09-02-2010)

  • #8
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    You, sir , are awesome. I was about to give up and get a script for a WYSIWYG editor. Thanks for the help. I did get an error with the last line of that php code. I avoided the error by doing this:

    $copypreview = str_replace("\&quot;", "\\&quot;", $copypreview);

    Is that going to get the same result?

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    No, that's not the same.

    You don't need or want to "escape" &quot; as it's already HTML-escaped. Putting a \ in front of it would make the slash appear in the text.

    I'm NOT a PHP programmer. Used it once for two days a couple of years ago.

    I thought I could do
    Code:
        $copypreview = str_replace("\"", "\\"", $copypreview);
    But if that doesn't work maybe you could try this:
    Code:
        $copypreview = str_replace('"', "\\".'"', $copypreview);
    I know that PHP allows " inside of '...' and ' inside of "..." so maybe that trick will work.

    Actually, though, you probably don't need that line, at all.

    Because you are plopping the PHP string into JavaScript between '....'

    SO if a " appear in there, it won't matter, same as PHP allows " in '...'
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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