0

私は2つのファイルを持っています:

edit.phtmlおよびpacked_data.php。後で外部テキスト ファイルに保存するために、JS 変数 ( packed_dat ) をpacked_data.php ファイルに投稿したいと考えています。

※つまり、クライアント側の変数packed_datをテキストファイルに保存したい。だから私はコードでこのような AJAX JQuery 呼び出しを行います.*

問題は、サーバーが POST の成功を通知しても、ファイルtest.txtが同じフォルダーに作成されておらず、テキスト データが保存されていないことです。

フォルダーのアクセス許可を確認しました。それらは 0777 です。それ以上何も言わずに Firebug を試しました。

edit.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>

pack_data.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ファイル内の関数を以下のコードに置き換えることができます。動作するはずです。

edit.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 に答える