1

I write test code, that post array on php page. Php page need write this array to file to check data.

JQuery:

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

In tests.php i tried to parse:

            $s = array();
            foreach ($_POST as $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 this code write only "1" every time. How fixed it?

4
  • Can you output the content of $_POST or $v ?? What does your $s variable look like when you save it into your file (after youyr foreach loop) ? Commented Jul 15, 2013 at 9:42
  • maybe send it as json and not pass ArrayCheckBox reference? Commented Jul 15, 2013 at 9:43
  • Not a JQuery expert but I would suggest that the use of square brackets in the name when POSTing is so it can be interpreted by PHP as an array before passing on to your PHP script. So the name of the $_POST item will be 'ids'. The whole if($v == 'ids[]') ... looks wrong to me. Surely you need something like if (!strcmp($k,'ids')) ... or using the less reliable "==", if ($k == 'ids') ... in your foreach $k will hold the element name/key and $v the value. Commented Jul 15, 2013 at 12:55
  • I also wondered why you appear to only be interested in the first element of your checkbox array ($v[0]). should you not be using a foreach to iterate through the array. Commented Jul 15, 2013 at 13:04

1 Answer 1

1

So, your javascript looks pretty close. The only thing that looks strange to me is that you used square brackets after ids[], I guess since it's an array -- you don't need to do that.

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

But I'm also a little confused about your variable ArrayCheckBox -- what is that supposed to contain? Is that an array of values you are ready to post? Or is that an actual Dom Object like a checkbox? If so, you need to get the raw data out first before you post it.

Moving on -- your PHP code confuses me a bit.

Once your javascript hits tests.php, PHP will kick off with your data in $_POST, it will looks something like this: array('ids'=>array('1','2','3',...)).

What do you want your file to look like? One of the easiest ways to take that whole array and write it to a file would be to use json:

<?php
$json = json_encode($_POST);
$file = $_SERVER['DOCUMENT_ROOT'] .'/test2.txt';
file_put_contents($file, $json);
?>

It looks like you were trying to check the data against what was already there, is that right? If so, you could do something like this:

<?php
$json = json_encode($_POST);
$file = $_SERVER['DOCUMENT_ROOT'] .'/test2.txt';
$current_contents = file_get_contents($file);
if ($current_contents == $json) {
    echo "Data is still the same as what was already there.";
}
else {
    echo "Data has changed."
}
?>

Let me know if you have any more questions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.