使用AJAX和php将内容保存到文件

时间:2021-04-10 09:56:10

Hi there and thanks a lot in advance. I'm messing with the following code for like ages and do not get it working. Actually the whole code should only write a DIV to a certain file on my server and give that file a name. Seems to be easy - but actually not to me as I realized.

您好,并提前感谢。我正在搞乱以下代码的年龄,并没有让它工作。实际上整个代码应该只将DIV写入我服务器上的某个文件并为该文件命名。似乎很容易 - 但实际上并不是我意识到的。

Here is my code so far:

这是我到目前为止的代码:

HTML / PHP:

HTML / PHP:

<div id="data2save">
<table width="80%" border="1px" cellpadding="0" cellspacing="0" style="float:left" >
<tbody>
  <tr>
    <td style="border-top:1px solid #000;border-left:1px solid #000;border-bottom:1px solid #000;">07:00 - 08:00</td>
    <td bgcolor="#99CC00" value="1">&nbsp;</td>
    <td bgcolor="#99CC00" value="2">&nbsp;</td>
    <td bgcolor="#99CC00" value="3">&nbsp;</td>
  </tr>
</tbody>
</table> 
</div>
<br>
<input type="button" value="save" id="save">

<?php
 $userid = 10;
 $kalenderwoche = date('W', time());
 sprintf("%02d",$kalenderwoche);
 $jahr = date('Y', time());
?>

AJAX:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

<script>

$("#save").live("click",function() {

var userid = "<?php echo $userid ?>";
var kalenderwoche = "<?php echo $kalenderwoche ?>";
var jahr = "<?php echo $jahr ?>";

var bufferId =$("#data2save").html();

        $.ajax({
             type : "POST",
             url : "saver2.php",
             data: {user_ID: userid , kw: kalenderwoche , jj : jahr , id : bufferId},
             dataType: "html",
             success: function(data){ 
             alert("ok");  
             }
             });
}); 
</script>

and the php file which handles the data to save it to a file and seems to contains the error.

以及处理数据以将其保存到文件的php文件,似乎包含错误。

saver2.php:

<?php

$kalenderwoche = $_POST['kw'];
$userid = $_POST['user_ID'];
$jahr = $_POST['jj'];
$data = $_POST['id'];

if (!file_exists($userid.'/')) {        
mkdir($userid.'/', 0755, true);
}

$copyname =  $userid. '/' .$userid. '_' .$jahr. '_' .$kalenderwoche. '.html';   

$handle = fopen($copyname , 'w+');  

if($handle)
 {

 if(!fwrite($handle, $data ))
 echo "ok";
 }


?>

Well, thats all. I hope anybody is sharp-eyed because I do not find the issue. Thank you

好吧,就是这样。我希望任何人都眼花缭乱,因为我找不到问题。谢谢

1 个解决方案

#1


0  

Shouldn't you close your file first? also, fwrite returns false in case of error, or the length of the file. so !fwrite() returns false if something ( or 0 ) had been written.

你不应该先关闭你的文件吗?另外,如果出现错误或文件长度,fwrite会返回false。所以!如果写了一些(或0),fwrite()返回false。

<?php

$kalenderwoche = $_POST['kw'];
$userid = $_POST['user_ID'];
$jahr = $_POST['jj'];
$data = $_POST['id'];

if (!file_exists($userid.'/')) {        
mkdir($userid.'/', 0755, true);
}

$copyname =  $userid. '/' .$userid. '_' .$jahr. '_' .$kalenderwoche. '.html';   

$handle = fopen($copyname , 'w+');  

if($handle)
 {

 if(fwrite($handle, $data ))
 echo "ok";
 }

fclose($handle);

?>

Also I hope you won't fetch that code to a production server as it's full of security holes.

此外,我希望您不会将该代码提取到生产服务器,因为它充满了安全漏洞。

#1


0  

Shouldn't you close your file first? also, fwrite returns false in case of error, or the length of the file. so !fwrite() returns false if something ( or 0 ) had been written.

你不应该先关闭你的文件吗?另外,如果出现错误或文件长度,fwrite会返回false。所以!如果写了一些(或0),fwrite()返回false。

<?php

$kalenderwoche = $_POST['kw'];
$userid = $_POST['user_ID'];
$jahr = $_POST['jj'];
$data = $_POST['id'];

if (!file_exists($userid.'/')) {        
mkdir($userid.'/', 0755, true);
}

$copyname =  $userid. '/' .$userid. '_' .$jahr. '_' .$kalenderwoche. '.html';   

$handle = fopen($copyname , 'w+');  

if($handle)
 {

 if(fwrite($handle, $data ))
 echo "ok";
 }

fclose($handle);

?>

Also I hope you won't fetch that code to a production server as it's full of security holes.

此外,我希望您不会将该代码提取到生产服务器,因为它充满了安全漏洞。