-3

I am new to JQuery and I use JSON stringify to pass my jquery array to the PHP but I've got an error. Can anyone help me to pass this jquery array into PHP?

JQUERY

    <script>
        $(document).ready(function(){
            let arraySelected = [];
            var arrayJSON = JSON.stringify(arraySelected);
            $(".btn").click(function(){      
                const find = jQuery.inArray($(this).val(),arraySelected);
                if(find !==-1){
                    arraySelected.splice(find, 1);
                    console.log($(this).val() +" has been deleted");
                    $(this).css("background-color", "");
                }else{
                    console.log($(this).val() +" has been pushed");
                    arraySelected.push($(this).val());
                    $(this).css("background-color", "red");

                    $.ajax({ 
                        type: "POST", 
                        url: "index.php", 
                        data: { myArray : arraySelected }, 
                        success: function() { 
                                alert("Success"); 
                            } 
                    }); 

                }
                console.log(arraySelected);
                
            })       
        });
    </script>

PHP

    <?php
        $myArray = json_decode($_POST['myArray']);
    ?>

This is the error shown on my webpage

Notice: Undefined index: myArray in C:\xampp\htdocs\JQuery PHP\JQuery PHP 1\index.php on line 13

  • 1
    whats the error? – Lawrence Cherone Apr 20 '22 at 11:28
  • 1
    _"but I've got an error."_ - _what_ error? You need to be precise, instead of just giving such vague non-info. – CBroe Apr 20 '22 at 11:29
  • 1
    What error message you get? Don't stringify content and there will be no need to decode it – Justinas Apr 20 '22 at 11:29
  • _"and I use JSON stringify to pass my jquery array to the PHP"_ - no, you are not ... The only thing JSON in your code is `arrayJSON`, but after you assigned a value to that (and not a very sensible one at that, you encoded an empty array as JSON), you are not doing anything further with that variable. What you are actually sending, is `arraySelected`. – CBroe Apr 20 '22 at 11:30
  • 1
    @CBroe also `myArray` wont be json, so `json_decode($_POST['myArray'])` wont work, OP need to let us know what the error is. – Lawrence Cherone Apr 20 '22 at 11:32
  • Sorry guys I forgot to add the error below, but I already edited that please check thank you. – Hide On Bush Apr 20 '22 at 11:39
  • 1
    When exactly does the undefined index error occur? Is index.php (where you send the AJAX) also your main page? If you see it in your page when you first load it (before the AJAX runs) then it indicates that you're not checking whether the request is actually a POST for looking for $_POST data (which inevitably will not exist in the current request). Personally I'd also suggest separating your code a bit and not sending AJAX requests to files which are normally used to load whole pages - it helps to avoid this kind of confusion. – ADyson Apr 20 '22 at 11:50

1 Answers1

-3

You decode an json object and not an array. See here: json-decode. You must call it like so

json_decode($json, true);

In your case

json_decode($_POST['myArray'], true);
Teemu
  • 22,058
  • 6
  • 53
  • 101
  • In his case it is the array that he has created in first instance. – Collie-IT Anne K. Frey Apr 20 '22 at 11:35
  • 1
    The issue is, that `$_POST['myArray']` is not in JSON format. JQuery parses the given data to a parameter list like so: `{ a: [1,2] } becomes the string "a%5B%5D=1&a%5B%5D=2"`, see https://api.jquery.com/jquery.ajax/#jQuery-ajax-url-settings – Teemu Apr 20 '22 at 11:40