0

Ok here we go. I am pulling a string list from a MySQL table data that looks like this:

1 Lumbar facet syndrome, bilateral
2 Bilateral knee arthropathy
3 Bilateral hip arthropathy
4 Right shoulder arthropathy, posttraumatic,
5 Chondromalacia of patella
6 Diabetes mellitus with diabetic nephropathy
7 Lower back pain
8 OA

I would like to retain this formatting as I need to put this information into a text box.

The program that I am working in has a pluggin system that allows the system to inject javascript into a form page. So the pluggin looks like this

 function newForm_javascript_onload() {

     //This part grabs the data from the MySQL table. 
    $sql = "SELECT dictation FROM form_dictation WHERE id = ? ORDER BY date DESC LIMIT 1";
    $stmDia = array($id);
    $list = sqlQuery($sql,$stmDia);
    $list = $list['dictation'];

     //Here I chop out the part of the text that I need for the text box
    $diaBegin = strpos($list, "Active Problems") + 26;
    $diaEnd = strpos($list, "Plan") - 4;
    $diaLength = strlen($list);
    $diaChop = ($diaEnd - $diaLength); 
    $diaNar = substr($list, $diaBegin, $diaChop);


    //This is the part that gets injected into the page
    echo "
         var diagnoses = '$diaNar';  //This is where formatting breaks the code

         //This part should populate the textarea

         if(form_diagFac1){
          document.getElementById('form_diagFac1').value = diagnoses;
         }


    ";


  }    

I have tried searching the forum but no one has posted a question like this.

Where the var is I have tried:

var diagnosis  = escape('$diaNar'); //This is where php passes data to java

I know I can strip out all the formatting so that it appears as a paragraph.

However, will I need to create a loop that reads each line?

1
  • The simplest way would be to use backticks instead of quotes (ES6 template string) but it would break in older browsers: var diagnoses = `$diaNar`; Commented Feb 14, 2017 at 14:23

1 Answer 1

0

Try json_encode. It adds "\r\n" into the string instead of newlines without breaking it and outputs the variable as a single string.

echo "var diagnoses = " . json_encode($diaNar) . ";
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.