I’m trying to make a “save as” feature using javascript and php. In the html I use:
<textarea id="filename" cols="20" rows="1"></textarea>
<li class ="add" onclick="js:saveas()" >save</li>
Then with inline javascript I do this to get the filename and dynamically generated dom:
<script>
function saveas() {
$.post('write.php', { name : $('#filename').val() });
$.post('write.php', { dom : $('html').html() });
}
</script>
Then over in “write.php”…
<?php
$name = urldecode($_POST['name']);
$dom = urldecode($_POST['dom']);
file_put_contents($name, $dom);
?>
But the console says:
[30-Aug-2012 15:45:22] PHP Notice: Undefined index: dom in /Users/J/Documents/test7/test7/write.php on line 3
[30-Aug-2012 15:45:22] PHP Notice: Undefined index: name in /Users/J/Documents/test7/test7/write.php on line 2
[30-Aug-2012 15:45:22] PHP Warning: file_put_contents() [<a href='function.file-put-contents'>function.file-put-contents</a>]: Filename cannot be empty in /Users/J/Documents/test7/test7/write.php on line 4
And it creates the file with the correct name but it’s empty, zero bytes. However if I hardcode the filename, like this:
file_put_contents('foo.html', $dom);
everything works the way it should, though I still get the notices. Any ideas on what I’m doing wrong?
Security issues aside (what you are doing is not secure), you are sending two different POSTs to write.php.
You want to send both values in 1 request:
And did I mention, what you are doing is not secure?