I created a part of my CMS that allows admins to convert all the text on the page to textareas, so they can edit the content. All the content they change is inside a div, and I submit via jQuery / AJAX the div's content to the PHP controller that processes the data. However, jQuery grabs the HTML just perfectly and sends it to the PHP controller, but the PHP butchers some of the content (removing some tags, every so often).
For example, if I submit
<div class="cmsedit" style="background-color:#EEE">Hello</div>
it will save/write the file with this
div class="cmsedit">Hello</div> .
It removes the opening bracket to the div and the b. It doesnt do it all the time. If I don't include a style tag, it generally leave the code just fine.
Here is the code I'm using on the javascript page.
$('.save_page').click(function(){
updated_content = $('.content_to_be_edited').html();
edit_page = $('#edit_page_name').val();
$.post("<?=site_url('admin/update_page_data') ?>", { content:updated_content,page_name:edit_page });
});
Here is the PHP Controller Page
$page_content = $this->input->post('content');
$page_name = $this->input->post('page_name');
$filename = $_SERVER['DOCUMENT_ROOT']."/application/views/".$page_name.".php";
// SAVE NEW FILE
$file = fopen($filename, 'w') or die("Can't open file");
$filedata = htmlspecialchars_decode($page_content);
fwrite($file, $filedata);
fclose($file);
Anyone have any ideas why the tags are getting all screwed up when the PHP processes the data? Thanks!!
Most likely you have the codeigniter XSS filter active which will destroy your data.
You can disable it and then it should work.
Related
I think I have a header related problem, am kinda new to them; here we go:
On 'summary.php' I have links that when I click, ajax a value to another page called 'note.php'.
Note.php has a header which i use to produce an MS-Word document - this works fine BUT ONLY WHEN I RUN note.php DIRECTLY, the Word file gets downloaded easily.
But no Word file download when I use the click-button-to-download-document.
Where should I look?
session_start();
include('otc_toolbox.php');
//ob_flush();
/*
if(isset($_POST["month"]))
{
$month = $_POST["month"];
$member= $_POST["member"];
}
*/
$month = $_POST["month"];
$member= $_POST["member"];
//else exit();
//Get current date
$date = date('Y-m-d');
//Prepare document html
$dnoteHTML = '';
$dnoteHTML .= '<html><body><table style="width:100%;">';
......
//header("Content-type: application/vnd.ms-word");
//header("Content-Disposition: attachment;Filename=DebitNote.doc");
echo $dnoteHTML;
You can't trigger a file download from an AJAX request.
With the content disposition set to attachment, using
window.location.href = "note.php";
should trigger the download without actually navigating away from the original page.
If the document takes a long time to generate, you could split the generation and download into two separate parts - AJAX request to a "generate" page to create the document, then when that's done, aim the browser at the "download" page to download it.
http://www.shedlimited.debrucellc.com
The site allows users to upload an image, via PHP script(ajaxupload.php) which is stored in $uploaded_img , inside of the php script. I want to pass the PATH of my newly stored image to a JS file, and the only way I've found to do it is by writing the value to a text file, which is extreme overkill
file_put_contents("testFile.txt", "");
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $upload_image;
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
fclose($fh);
At the same time, I have an external .js file running which loads my html5 Canvas etc,
I set a global variable sourcer , inside the external .js file, which I'm able to access and update from my index.php by reading the URL from the file which I wrote the url to in my php script.:
jQuery.get('scripts/testFile.txt', function(data) {
sourcer = data;
});
Is there any way that I can pass the URL value to sourcer without having to manually insert it in a text file?
Add a block to your head on your php template.
<script type="text/javascript">
var uploaded_img = '<?php echo json_encode($uploaded_img); ?>';
</script>
The json encode makes sure the variable is properly encoded, even if it is an object or array.
If it is purely an ajax thing. Just return the filename in your ajax response.
So post to your ajax upload, and make your ajax script return a json object with the filename.
So in your upload script, at he bottom:
header('Content-type: application/json');
echo json_encode(array('filename'=>$uploaded_img));
And read the response.filename in javascript.
What about using a session variable to store the uploaded image AND an ajax response to work with ?
I see an advantage to use both : you can use the value directly on the upload page right after the image is uploaded. And for the session var, it will ensure that the server side is always able to get the value if you ever need to access it from another context.
REVISED QUESTION:
have an xml document, i wish to change the qty of a book in the xml to increment by 1 on command. is there anyway of updating an XML document through the web.
Many Thanks
You can use codemirror editor, to edit your file and you need to implement saving in php on server side. You can post the content using jquery
$.post('your_script.php', {file: myCodeMirror.getValue()}, function() {
// your file was save
});
and in php
if (isset($_POST['file'])) {
$f = fopen('your.xml', 'w')
fwrite($f, $_POST['file']);
fclose($f);
}
Here is what i was getting at:
http://www.php.net/manual/en/domdocument.save.php
the save method allows you to save it to the webserver, i will use php to open it and then save and close it with this
I've got a large form where the user is allowed to input many different fields, and when they're done I need to send the contents of the form to the server, process it, and then spit out a .txt file containing the results of the processing for them to download. Now, I'm all set except for the download part. Setting the headers on the response to the jQuery .post() doesn't seem to work. Is there any other way than doing some sort of iframe trick to make this work (a la JavaScript/jQuery to download file via POST with JSON data)?
Again, I'm sending data to the server, processing it, and then would like to just echo out the result with headers to prompt a download dialog. I don't want to write the result to disk, offer that for download, and then delete the file from the server.
Don't use AJAX. There is no cross-browser way to force the browser to show a save-as dialog in JavaScript for some arbitrary blob of data received from the server via AJAX. If you want the browser to interpret the results of a HTTP POST request (in this case, offering a download dialog) then don't issue the request via AJAX.
If you need to perform some kind of validation via AJAX, you'll have to do a two step process where your validation occurs via AJAX, and then the download is started by redirecting the browser to the URL where the .txt file can be found.
Found this thread while struggling with similar issue. Here's the workaround I ended up using:
$.post('genFile.php', {data : data}, function(url) {
$("body").append("<iframe src='download.php?url="+url+"' style='display: none;'></iframe>");
});
genFile.php creates the file in staging location using a randomly generated string for filename.
download.php reads the generated file, sets the MIME type and disposition (allowing to prompt using a predefined name instead of the random string in the actual filename), returns the file content and cleans up by deleting the source file.
[edit] might as well share the PHP code...
download.php:
<?php
$fname = "/tmp/".$_GET['url'];
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="plan.xml"');
echo file_get_contents($fname);
unlink ($fname);
?>
genFile.php:
<?php
$length = 12;
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = substr( str_shuffle( $chars ), 0, $length ).'.xml';
$fh = fopen(('tmp/'.$str), 'w') or die("can't open file");
fwrite($fh,$_POST["data"]);
fclose($fh);
echo $str;
?>
Rather than using jQuery's .post(), you should just do a normal POST by submitting the form, and have the server respond with appropriate Content-Encoding and MIME-type headers. You can't trigger a download through post() because jQuery encapsulates the returned data.
One thing I see in use rather frequently, though, is this:
$.post('generateFile.php', function(data) {
// generateFile builds data and stores it in a
// temporary location on the server, and returns
// the URL to the requester.
// For example, http://mysite.com/getFile.php?id=12345
// Open a new window to the returned URL which
// should prompt a download, assuming the server
// is sending the correct headers:
window.open(data);
});
I would like to make a form that once you insert data into the form, it will automatically create a html format of the data you entered in a html and save it in a folder! How do i go by doing this?
No idea what you mean by "html format of the data", but having PHP write a file is trivial:
$data = "your html format goes here";
$fh = fopen('the_file_you_want_to_write_to.html', 'wb') or die('unable to open file for output');
fwrite($fh, $data);
fclose($fh);
Or even more simplistically:
file_put_contents('the_file_you_want_to_write_to.html', $data);
You could simply place a wysiwyg html javascript editor (such as TinyMCE ) inside a form (textarea field), and store the output in a file with Php.
Some basic WIKI's do only that.