0

I am a beginner in jquery. I'm sorry for my english. I read data from mysql:

$id=($row["ID"]);  
$name=($row["Name"]);
$note=($row["Note"]);  *// note set to textarea*

send function:

onclick= modP($id,$name,$note);

<script>
 function modP(id,name,note){
 $("#NoteP").val(note);
}
</script>

----- PROBLEM -------

NoteP is textarea.

if $note has one line the function work. all OK.

if $note has many lines the function modP is not called.

I tried with:

$note = str_replace("\n","<br />", $row["Note"]);  

Not work!

Please help me. Thank you.

Rp9
  • 1,955
  • 2
  • 23
  • 31

5 Answers5

1

Try to encode your string to JSON in the PHP part then decode in JS part.

PHP :

$note= json_encode($row["Note"]);

JS :

$("#NoteP").val( JSON.parse(note) );
Mohammed Acharki
  • 234
  • 2
  • 14
  • I have tried as he said, the function modP is not called. – carmeloconny Sep 27 '18 at 11:24
  • Add the whole tag where you've `onclick= modP($id,$name,$note);` attribute – Mohammed Acharki Sep 27 '18 at 12:00
  • $note=($row["Note"]); echo(" "); – carmeloconny Sep 27 '18 at 15:48
  • – carmeloconny Sep 27 '18 at 15:55
  • I have tried a lot, but it does not work. $note = json_encode($row["Note"], JSON_HEX_QUOT | JSON_HEX_TAG); -------> modPaziente('".$note."')\"> "); – carmeloconny Sep 29 '18 at 11:37
0

use nl2br on $note to break multines as below.

$id=$row["ID"];  
$name=$row["Name"];
$note=$row["Note"]; 
$note=nl2br($note);

You can call onclick function on your html element as below.

<button onClick="modP('<?php echo $id ?>','<?php echo $name ?>','<?php echo $note ?>');">Click</button> 

And the modP function will be as shown below.

<script type="text/javascript"> 
function modP(id,name,note) { 
    $("#NoteP").val(note);;
} 
</script> 

I hope this helps.

0

I found this suggestion Link

Jquery function does not accept variables on multiple lines.

$note=str_replace("\n","<br />",$row["Note"]);

Now the function is called correctly. ($note) is one line. How can I write textarea restoring original text, with many lines?

Community
  • 1
  • 1
0
$note = json_encode($row["Note"], JSON_HEX_QUOT | JSON_HEX_TAG);   
// note set to textarea - multiline

pass $data to the function modPaziente:

 echo("<td><input name=\"imgmodifica\" id=\"imgmodifica\" type=\"image\" src=\"image/modifica2.png\" width=\"24\" height=\"24\" title=\"Elimina ".$row["ID_P"]."\" onclick=\"return modPaziente('$id','$nom','$ind','$comID','$citta','$prov','$tel','$cf','$ese','$cons','$nato','$sesso','".$note."','$eta')\"> </input></td>"); 

JS - /* alert(note) NOT work */

<script type="text/javascript"> 
function modPaziente(id,nom,ind,comID,citta,prov,tel,cf,ese,cons,nato,sesso,note,eta){ 
alert(note); 
} 
</script> 

why modPaziente does not read note?

0

I solved that with htmlspecialchars and json_encode :

  $note = htmlspecialchars(json_encode($row["Note"], JSON_HEX_QUOT | JSON_HEX_TAG));  

...without quotes $note

onclick="return modPaziente($note);"

....

<script type="text/javascript">
function modPaziente(note){
    alert(note);
}

Thank you all for the suggestions