Posting the value of a textarea with jquery

HedleyVargas

New Member
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!
 
Back
Top