简体   繁体   中英

Posting the value of a textarea with jquery

I am looking for a (correct) way to read and write to a linux text file with javascript, jQuery and php. Specifically, I want to take the value of a (#taFile) with jQuery ($("#taFile").val();) and $.post it to a php script, which in turn writes the posted value to a text file.

While my code mostly works, I have run into a snag: when taking the value of the textarea and $.posting it, all special characters are lost. Specifically, 1) all special characters are lost, 2) new lines are converted to a space and 3) all characters after certain special characters (I've noticed it happen with a #) are lost.

These are still rough drafts of the script however I'm very open to any improvements and suggestions!

Javascript:

$(document).ready(function() {
 $("input:button").button();
 $("#tabs").tabs();

 $("#tabs").tabs({
  select: function(event, ui) { // When a tab is selected
   file = ui.panel.id;
   console.log("js: file: "+file);
   if (file == "items" || file == "groups" || file == "whitelist" || file == "users"){ // if file is valid
    $("#taFile").remove(); // Remove any old textareas
    $("#"+file).html(''); // Add a new textarea
    $.post("/php/getFile.php?action=read&file="+file, function(data){ // Post with the current file
     $("#taFile").val(data); // Put file into textarea
    });
   }
  }
 });

 $("#btnSubmit").click(function() {
  var newcfg = $("#taFile").val();
  alert(newcfg); // This shows that newcfg preserves the exact value of the textarea
  console.log(newcfg); // As does this.
  $.post("/php/getFile.php?action=write&file="+file+"&newcfg="+newcfg); // This shows new lines (\n ?) as " " (a space) and removes all special characters (! , # ; : etc)
 });
});

PHP:

$file = $_REQUEST['file'];
 $newcfg = $_REQUEST['newcfg'];
 $action = $_REQUEST['action'];
 $user = 'public';
 $config = "/home/$user/mc/$file.txt";

 if ($action == "read"){
  $oldcfg = file_get_contents($config);
  echo $oldcfg;
 } else if ($action == "write") { #if writing
  $fh = fopen($config, 'w') or die("Cant write to file"); #open file
  fwrite($fh, $newcfg); #write file
  fclose($fh); #close file
 }

I had to remove the php tags as they caused the actual PHP code to not show up. Not that some of the script is for reading the file in first, when changing tabs. That all works fine.

Thanks!

您可能想查看PHP htmlentitieshtmlspecialcharacters

Note: You are using jQuery.post() and setting your data as a $_GET parameters . It should be called like;

$.post("/php/getFile.php", { action: "write", file: file, newcfg: newcfg },
   function(data){
     alert("Data Loaded: " + data);
   });

You can keep your line breaks with a function like this:

function splitter($content) {
$content = str_replace(chr(10),'<br/>',$content);
$content = str_replace("<br/><br/><br/><br/>","<br/><br/>",$content);
return $content;
}

Which you run like:

$newcfg = splitter($newcfg);

And typoknig is right abouit special chars

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM