0

I have a little bit of a problem, I have a JavaScript with jQuery where I work with an Array I got from PHP, in the script I add some data to two new arrays and here comes my problem, how can I work with those two arrays in my PHP file? I need to save the data from the two new arrays in a database. In the PHP I have a form where I enter some data about a User. Does someone knows a easy way to send my data back to the PHP?

Some information about the script: I have a table with the names of schools, and a checkbox with the id of the school as a value. when i check or uncheck one of the checkboxes the script checks if it is a school already saved in the database for this specific user or if it's a new user.

    <script>
        var schools = [];
        var oldschools = [];
        var uncheckedschools = [];
        oldschools = <?php echo json_encode($oldschoolids); ?>;

        $(".checkbox").change(function() {
            if(this.checked) {
                var additem = $(this).val();
                for (var i=0; i<oldschools.length; i++) {
                    if (additem == oldschools[i]) {
                        uncheckedschools = jQuery.grep(uncheckedschools, function(value) {
                            return value != additem;
                        });
                    }
                }
                schools.push(additem);
            } else {
                var removeitem = $(this).val();
                for (var i=0; i<oldschools.length; i++) {
                    if (removeitem == oldschools[i]) {
                        uncheckedschools.push(removeitem);
                    }
                }
                schools = jQuery.grep(schools, function(value) {
                    return value != removeitem;
                });
            }
        });
    </script>

I hope someone can help me with this problem!

2
  • Use $.ajax in the JS and json_decode in your PHP? Commented May 30, 2013 at 13:47
  • You need to somehow send your array in javascript over to PHP as a string, and then use json_decode to make it into a PHP array. Commented May 30, 2013 at 13:50

1 Answer 1

4

You'll need to use AJAX to send your updates back to your server. Using jQuery's ajax() method, it would looks something like this:

$.ajax({
    url: 'path/to/serverside/file.php',
    dataType: 'json',
    data: {schools: schools},
    success: function(dataFromServer) {
        //after the save is successful, you can do something here 
    },
    error: function(dataFromServer) {
        //if there was an error handle it here
    }
});

EDIT: As mentioned by a few commentors, you'll need to use json_decode on the server-side to decode the JSON Object you're sending back: http://php.net/manual/en/function.json-decode.php

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

5 Comments

I think they are trying to turn the "oldschools" object back into a PHP array. PHP is serverside and JS is client side so you have to POST the data back to the server after the client is done with it. The best way to achieve that is with AJAX. The example above is a good example
worth mentioning that on the php side you'll want to use json_decode to convert the json string to a php variable - otherwise it probably won't be very useful.
If there are objects in the json array, also make sure you use json_decode($var, TRUE); with PHP to include associations.
First of all thank you very much for the fast answer. But I think it doesn't solve my whole problem. If I call my PHP-File where I save everything to the database, the other data I typed in will be lost or more specifically I will call my database functions everytime I check/uncheck on of the checkboxes. Is it possible that I get the values from my uncheckedschools and schools array when I click on the Submit-Button from the form in my PHP?
I'm not entirely clear why AJAX won't work for you, but if you want to send data as a form post, you can set the arrays as a value in a hidden form field. Something like $('#unchecked-hidden').val(uncheckedschools.join(','));. Then you could parse the comma-delimited list on the server and save the values.

Your Answer

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