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("
","<br />", $row["Note"]);
Not work!
Please help me. Thank you.
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
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) );
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.
I found this suggestion Link
Jquery function does not accept variables on multiple lines.
$note=str_replace("
","<br />",$row["Note"]);
Now the function is called correctly. ($note) is one line. How can I write textarea restoring original text, with many lines?
$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?