[英]Trying to make a “save as” form using php's “file_put_contents” and javascript… “Filename cannot be empty”
我正在尝试使用javascript和php进行“另存为”功能。 在html中,我使用:
<textarea id="filename" cols="20" rows="1"></textarea>
<li class ="add" onclick="js:saveas()" >save</li>
然后,使用内联javascript来获取文件名和动态生成的dom:
<script>
function saveas() {
$.post('write.php', { name : $('#filename').val() });
$.post('write.php', { dom : $('html').html() });
}
</script>
然后在“ write.php”中...
<?php
$name = urldecode($_POST['name']);
$dom = urldecode($_POST['dom']);
file_put_contents($name, $dom);
?>
但是控制台说:
[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
它会使用正确的名称创建文件,但它是空的零字节。 但是,如果我对文件名进行硬编码,如下所示:
file_put_contents('foo.html', $dom);
一切都按应有的方式进行,尽管我仍然收到通知。 关于我在做什么错的任何想法吗?
除了安全性问题(您所做的事情并不安全),您还发送了两个不同的POST到write.php 。
您要在1个请求中发送两个值:
$.post('write.php', {
name : $('#filename').val(),
dom : $('html').html()
});
我是否提到过,您在做什么并不安全?
你为什么$.post
2个$.post
电话? 您需要在一次调用中发送两个变量。
$.post('write.php', {
name : $('#filename').val(),
dom : $('html').html()
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.