How to POST a Javascript Variable (From Client Side) to PHP File (Server Side) with AJAX using JQUERY -


i have 2 files:

edit.phtml , packed_data.php. want post js variable (packed_dat) packed_data.php file in order save later external text file.

*in other words want save client-side variable packed_dat text file. ajax jquery call in code.*

the problem although server informs me successful post, file test.txt not being created same folder, , no text data being saved.

i have checked folder permissions, 0777. have tried firebug without telling me more.

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 $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 $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 $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 $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> 

packed_data.php:

<?php  echo 'ok';   if(isset($_post['packed_dat'])) {     $uid = $_post['packed_dat'];      // whatever want $uid }  $dir = 'mydir';   // create new directory 777 permissions if not exist yet  // owner user/group php script run under  if ( !file_exists($dir) ) {   mkdir ($dir, 0777);  }  file_put_contents ($dir.'/test.txt', $uid);  ?> 

i appreciate help... in advance!!!

there many different things going wrong here. first, think jquery's $.post snippet wrong (you're missing matching curvy bracket , semicolon @ end) find weird server informs successful post. i'd change this:

function() {      jquery.post( 'packed_data.php', {'packed_dat': packed_dat},         function() {             alert('write ok!'); }) 

into this:

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

and then, in packed_data.php script, give $uid default value in case $_post['packed_dat'] isn't set, still creates file or not (and distinguish whether problem in server side or in client side).


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -