我有两个文件:
edit.phtml和packed_data.php。我想将 JS 变量(packed_dat)发布到 packed_data.php 文件中,以便稍后将其保存到外部文本文件中。
*换句话说,我想将客户端变量 packed_dat 保存到文本文件中。所以我在代码中做了一个这样的 AJAX JQuery 调用。*
问题是尽管服务器通知我 POST 成功,但文件test.txt并没有被创建到同一个文件夹中,也没有保存文本数据。
我检查了文件夹权限,它们是 0777。我尝试了 Firebug,但没有告诉我更多信息。
编辑.phtml:
<div class="content-header">
<table cellspacing="0">
<tr>
<td style="width:50%;"><h3 class="icon-head head-products">Manage maps</h3></td>
<td class="a-right">
<button style="" onclick="sendData()" class="scalable save" type="button"><span>Save</span></button>
<button style="" onclick="what()" class="scalable" type="button"><span>Show data</span></button>
<button type="button" class="scalable back" onclick="javascript:history.back()"><span>Cancel</span></button>
</td>
</tr>
</table>
</div>
<?php
//$this->debug();
$droppables = $this->getDroppable();
$draggables = $this->getDraggable();
?>
<div id="map_body">
<div id="map_left">
<div id="drop_unmapped" style="height: <?php echo count($draggables)*30+70; ?>px;" class="ui-widget-header drop big">
<p>XML fields</p>
<?php foreach($draggables as $key => $value) {
echo '<div id="drag_'.$value['name'].'" class="ui-widget-content drag">' .PHP_EOL;
echo $value['name'] .PHP_EOL;
echo '</div>' .PHP_EOL;
}?>
</div>
</div>
<div id="map_right">
<?php foreach($droppables as $value) {
?>
jQuery("#drop_<?php echo $value->getIdentifier(); ?>").droppable({
drop: function(event, ui) {
jQuery(this).addClass('ui-state-highlight');
for(key in fields){
if(fields[key] == jQuery(ui.draggable).html()){
fields[key] = 0;
}
}
fields["<?php echo $value->getIdentifier(); ?>"] = (jQuery(ui.draggable).html());
},
out: function(event, ui) {
jQuery(this).removeClass('ui-state-highlight');
}
});
<?php
}
?>
jQuery("#drop_unmapped").droppable({
drop: function(event, ui) {
for(key in fields){
if(fields[key] == jQuery(ui.draggable).html()){
fields[key] = 0;
}
}
}
});
});
</script>
<script type="text/javascript">
jQuery(function() {
<?php
foreach($draggables as $key => $value){
?>
jQuery("#drag_<?php echo $value['name']; ?>").draggable({
revert: 'invalid',
snap: '.drop',
snapMode: 'inner',
snapTolerance: 10,
drag: function(event, ui) {jQuery(this).draggable('option', 'zIndex', 10000);}
});
<?php
}
?>
});
var fields=new Object();
<?php
foreach($droppables as $value){
echo 'fields["'.$value->getIdentifier().'"] = 0;' . PHP_EOL;
}
?>
function what(){
var string ='';
for(key in fields) {
string += (key + '=' + fields[key] + '\n');
}
alert(string);
}
function sendData()
{
var packed = "";
packed = jQuery.toJSON(fields);
alert(packed);
var packed_dat = "test123";
alert(packed_dat);
function() {
jQuery.post( 'packed_data.php', {'packed_dat': packed_dat},
function() {
alert('Write OK!');
})
alert(packed_dat);
document.data.data.value = packed;
document.data.submit();
}
</script>
打包数据.php:
<?php
echo 'ok';
if(isset($_POST['packed_dat']))
{
$uid = $_POST['packed_dat'];
// Do whatever you want with the $uid
}
$dir = 'myDir';
// create new directory with 777 permissions if it does not exist yet
// owner will be the user/group the PHP script is run under
if ( !file_exists($dir) ) {
mkdir ($dir, 0777);
}
file_put_contents ($dir.'/test.txt', $uid);
?>
我会很感激任何帮助...提前谢谢!!!