Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to POST a Javascript Variable (From Client Side) to PHP File (Server Side) with AJAX using JQUERY
    primarykey
    data
    text
    <p>I have two files:</p> <p><strong>edit.phtml</strong> and <strong>packed_data.php</strong>. I want to post the JS variable (<strong>packed_dat</strong>) to packed_data.php file in order to save it later to an external text file.</p> <p>*In other words i want to save the client-side variable packed_dat into a text file. So i do an AJAX JQuery call like this in the code.*</p> <p>The problem is that although the server informs me about a successful POST, the file <strong>test.txt</strong> is not being created into the same folder, and no text data is being saved.</p> <p>I have checked the folder permissions, they are 0777. I have tried Firebug without telling me anything more.</p> <p><strong>edit.phtml:</strong></p> <pre><code>&lt;div class="content-header"&gt; &lt;table cellspacing="0"&gt; &lt;tr&gt; &lt;td style="width:50%;"&gt;&lt;h3 class="icon-head head-products"&gt;Manage maps&lt;/h3&gt;&lt;/td&gt; &lt;td class="a-right"&gt; &lt;button style="" onclick="sendData()" class="scalable save" type="button"&gt;&lt;span&gt;Save&lt;/span&gt;&lt;/button&gt; &lt;button style="" onclick="what()" class="scalable" type="button"&gt;&lt;span&gt;Show data&lt;/span&gt;&lt;/button&gt; &lt;button type="button" class="scalable back" onclick="javascript:history.back()"&gt;&lt;span&gt;Cancel&lt;/span&gt;&lt;/button&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;?php //$this-&gt;debug(); $droppables = $this-&gt;getDroppable(); $draggables = $this-&gt;getDraggable(); ?&gt; &lt;div id="map_body"&gt; &lt;div id="map_left"&gt; &lt;div id="drop_unmapped" style="height: &lt;?php echo count($draggables)*30+70; ?&gt;px;" class="ui-widget-header drop big"&gt; &lt;p&gt;XML fields&lt;/p&gt; &lt;?php foreach($draggables as $key =&gt; $value) { echo '&lt;div id="drag_'.$value['name'].'" class="ui-widget-content drag"&gt;' .PHP_EOL; echo $value['name'] .PHP_EOL; echo '&lt;/div&gt;' .PHP_EOL; }?&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="map_right"&gt; &lt;?php foreach($droppables as $value) { ?&gt; jQuery("#drop_&lt;?php echo $value-&gt;getIdentifier(); ?&gt;").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["&lt;?php echo $value-&gt;getIdentifier(); ?&gt;"] = (jQuery(ui.draggable).html()); }, out: function(event, ui) { jQuery(this).removeClass('ui-state-highlight'); } }); &lt;?php } ?&gt; jQuery("#drop_unmapped").droppable({ drop: function(event, ui) { for(key in fields){ if(fields[key] == jQuery(ui.draggable).html()){ fields[key] = 0; } } } }); }); &lt;/script&gt; &lt;script type="text/javascript"&gt; jQuery(function() { &lt;?php foreach($draggables as $key =&gt; $value){ ?&gt; jQuery("#drag_&lt;?php echo $value['name']; ?&gt;").draggable({ revert: 'invalid', snap: '.drop', snapMode: 'inner', snapTolerance: 10, drag: function(event, ui) {jQuery(this).draggable('option', 'zIndex', 10000);} }); &lt;?php } ?&gt; }); var fields=new Object(); &lt;?php foreach($droppables as $value){ echo 'fields["'.$value-&gt;getIdentifier().'"] = 0;' . PHP_EOL; } ?&gt; 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(); } &lt;/script&gt; </code></pre> <p><strong>packed_data.php:</strong></p> <pre><code>&lt;?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); ?&gt; </code></pre> <p>I would appreciate any help... Thanks in advance!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload