JQuery - post array and write to file on php -


i write test code, post array on php page. php page need write array file check data.

jquery:

    $("#trash").click(function () {     $.post("tests.php",     {'ids[]': arraycheckbox},         function(result){             window.location = 'tests.php';          }       );     }); 

in tests.php tried parse:

            $s = array();             foreach ($_post $k => $v) {               if (is_array($v)) {                 if ($v=='ids[]')                   array_push($s, $v[0]);               }             }              $file = $_server['document_root'] .'/test2.txt';             $current = file_get_contents($file);             $current .= implode(', ', $s);              file_put_contents($file, $current); 

but code write "1" every time. how fixed it?

so, javascript looks pretty close. thing looks strange me used square brackets after ids[], guess since it's array -- don't need that.

$("#trash").click(function () {     $.post("tests.php",         {'ids': arraycheckbox},         function(result){             window.location = 'tests.php';         }     ); }); 

but i'm little confused variable arraycheckbox -- supposed contain? array of values ready post? or actual dom object checkbox? if so, need raw data out first before post it.

moving on -- php code confuses me bit.

once javascript hits tests.php, php kick off data in $_post, looks this: array('ids'=>array('1','2','3',...)).

what want file like? 1 of easiest ways take whole array , write file use json:

<?php $json = json_encode($_post); $file = $_server['document_root'] .'/test2.txt'; file_put_contents($file, $json); ?> 

it looks trying check data against there, right? if so, this:

<?php $json = json_encode($_post); $file = $_server['document_root'] .'/test2.txt'; $current_contents = file_get_contents($file); if ($current_contents == $json) {     echo "data still same there."; } else {     echo "data has changed." } ?> 

let me know if have more questions.


Comments