0

I have the following :

<script charset="UTF-8">
function deleter(theid) {
    var namme = document.getElementById(theid).id;
    $.post( "sql_machine.php",  { 
        selection_name: select_namme
    })
}
</script>

using jquery, would it be possible to post a php array too? may be encoding it as json? like the following?

<script charset="UTF-8">
function deleter(theid) {
    var select_namme = document.getElementById(theid).id;
    $.post( "sql_machine_tomskus.php",  { 
        selection_name: select_namme,
        { array : dataToSend }
    })
}
</script> 
3
  • Yes you can send an array. Commented Feb 20, 2015 at 15:58
  • what did u mean with php array ? dataToSend is an array comming from php ? Commented Feb 20, 2015 at 15:59
  • yes I have a php array that I'd like to post trough an AJAX Commented Feb 20, 2015 at 16:00

2 Answers 2

1

Just use JSON.stringify to send the array, and to decode it to array in php, use json_decode.

In JQ:

<script charset="UTF-8">
function deleter(theid) {
    var select_namme = document.getElementById(theid).id;
    $.post( "sql_machine_tomskus.php",  { 
        selection_name: select_namme,
        array : JSON.stringify(yourArrayOrObject)
    })
}
</script>

Then, in php just use json_decode($_POST["array"])

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

2 Comments

thanks and how could I "send" the array to the function?? Also shoud I jsonencode it?
@markoc. just save the array into yourArrayOrObject or place it right there... var yourArrayOrObject = {name: 'vlad'};, or you can place the array right inside the AJAX call: array : JSON.stringify({name: 'vlad'})
0

thanksGINCHER here it is my final solution :

    <script charset="UTF-8">
        function deleter(theid) {
            var select_namme = document.getElementById(theid).id;
            $.post( "sql_machine.php",  { 
                selection_name: select_namme,
                array : <?echo json_encode($array);?>
            })
        }
    </script>

2 Comments

Both JSON.stringify and json_encode do the same action. If you already have it in JSON you don't have to convert it to JSON. Just do it like this: array : "<?echo json_encode($array);?>"
Add error checking if this is intended for a production environment. You can thank me later ;)

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.