0

我有两个文件:

edit.phtmlpacked_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);

?>

我会很感激任何帮助...提前谢谢!!!

4

2 回答 2

0

您为packed_data.php文件编写的代码似乎是正确的,所以我认为问题在于通过 AJAX 传递参数值。所以你可以用下面的代码替换文件中的函数sendData()edit.phtml我认为它应该可以工作。

编辑.phtml

function sendData()
{
    var packed = "";
    packed = jQuery.toJSON(fields);
    alert(packed);

    var packed_dat = "test123";
    alert(packed_dat);

    $.ajax({
        type: 'POST',
        url: 'packed_data.php',
        data: 'packed_dat='+packed_dat,
        success: function(msg) {
            alert('success');
        }
    });
}
于 2013-10-01T09:43:50.870 回答
0

这里可能会出现许多不同的问题。首先,我认为您的jQuery片段$.post是错误的(您缺少匹配的弯曲括号和末尾的分号),所以我觉得您的服务器通知您成功的POST. 我会改变这个:

function() {
     jQuery.post( 'packed_data.php', {'packed_dat': packed_dat},
        function() {
            alert('Write OK!');
})

进入这个:

$.post('packed_data.php', { packed_dat: packed_dat }, function (data) {
    alert('Write OK!');
});

然后,在您的packed_data.php脚本中,提供$uid一个默认值,以便在$_POST['packed_dat']未设置的情况下,它仍然会创建一个文件并由此区分问题是在服务器端还是在客户端)。

于 2013-08-09T20:17:39.200 回答