WEB String passing in JavaScript

Discussion in 'OT Technology' started by Fase, Apr 19, 2010.

  1. Fase

    Fase Your Face, In A Pickle Jar.

    Joined:
    Apr 6, 2004
    Messages:
    29,540
    Likes Received:
    0
    Location:
    Windsor, Ont, Canada.
    Basically I have an OnClick which calls a JavaScript function which pops up a window and fills in a textarea with text.

    Problems is with multiple lines.

    Works (single line):
    Code:
    onclick="Show('blah')" 
    
    Doesn't (multi line):
    Code:
    onclick="Show('blah
    blah
    blah')" 
    
    The text for the function is written in via PHP and pulled from a database so some of the database entries contain multiple lines.

    So how would I go about being able to pass multiple lines?



    EDIT: Here's some source to help me explain

    JavaScript
    Code:
    <script type="text/javascript">
    
    function Hide() {
    	var el = document.getElementById('overlay');
    	el.style.display = 'none';
    }
    
    function Show(hours, details, date) {
    	var el = document.getElementById('overlay');
    	var text = document.getElementById('details');
    	el.style.display = 'block';
    	document.frmMain.date.value = date;
    	document.frmMain.hours.value = hours;
    	text.focus();
    	text.value = details;
    }
    
    </script>
    
    PHP that adds onclick to table cells
    Code:
    $onClick = 'onclick="Show(' . $hours . ', ' . '\'' . $details . '\'' . ', ' . $today . ')"';
    
    $details is assigned with data pulled from a database, which if it contains more than one line results in new lines in my source when generated, and thus a JavaScript error. Single line works fine.
     
    Last edited: Apr 19, 2010
  2. Supergeek

    Supergeek New Member

    Joined:
    Jan 23, 2007
    Messages:
    1,855
    Likes Received:
    0
    Location:
    Colorado
    Code:
    onclick="Show(string_variable)";
     
  3. Fase

    Fase Your Face, In A Pickle Jar.

    Joined:
    Apr 6, 2004
    Messages:
    29,540
    Likes Received:
    0
    Location:
    Windsor, Ont, Canada.
    Alright, then how to I go about taking the multi line string pulled from a database via PHP and saving it to a JavaScript variable..? Seems like the same problem.
     
  4. Fase

    Fase Your Face, In A Pickle Jar.

    Joined:
    Apr 6, 2004
    Messages:
    29,540
    Likes Received:
    0
    Location:
    Windsor, Ont, Canada.
    Looks like it just replaces \n with <br /> which is no good. The source was still generated with new lines too.

    I'm trying to take multi line text out of a database via PHP, and have it ready for a JavaScript function.
     
  5. Fase

    Fase Your Face, In A Pickle Jar.

    Joined:
    Apr 6, 2004
    Messages:
    29,540
    Likes Received:
    0
    Location:
    Windsor, Ont, Canada.
    And do what? That database data for something like

    blah
    blah
    blah

    PHP would see 'blah\n\rblah\n\rblah\n\rblah'? So I am confused how doing a replace will help. :confused:
     
  6. Fase

    Fase Your Face, In A Pickle Jar.

    Joined:
    Apr 6, 2004
    Messages:
    29,540
    Likes Received:
    0
    Location:
    Windsor, Ont, Canada.
    Just tried this and it's not removing the line breaks.
     
  7. Fase

    Fase Your Face, In A Pickle Jar.

    Joined:
    Apr 6, 2004
    Messages:
    29,540
    Likes Received:
    0
    Location:
    Windsor, Ont, Canada.
    Looks like I got it to work with
    Code:
    $details = str_replace(sprintf('%s%s', chr(13), chr(10)), '\r\n', $details);
    
     

Share This Page