skip to Main Content

So im trying to send an object array to a js function anytime I push a button.

<button onclick="actualizarProcesos(<?php echo json_encode($p_array)?>);">X</button>

I’ve made sure my json isnt sending any weird characters as it is mostly int except for the object attribute “name” which is string.

That function is in a different js file:

   function actualizarProcesos(p_array){
    var p = JSON.parse(p_array);
}

At the moment I’m trying to send make sure the function is receiving the data but it keeps throwing the error Uncaught SyntaxError: Unexpected end of inputSo I’m stuck trying to find out how to fix the error.

I plan afterwards on sending that array to another php file using ajax, something like this:

$.ajax({
            type: "POST",
            url: "post.php",
            data: JSON.stringify(values),
            success: function(data){
                alert(data)
            }
    });

This is the full json I’m trying to send

[{"name":"A","t_arrival":7,"t_est":25,"state":1,"pages":5,"mem_data":[[1,8,13,5,0,1],[0,0,0,0,0,0],[1,11,17,3,1,1],[1,12,16,4,0,1],[0,0,0,0,0,0]],"t_rem":25,"t_wait":0,"rem_quantum":0},{"name":"B","t_arrival":6,"t_est":13,"state":2,"pages":4,"mem_data":[[0,0,0,0,0,0],[1,9,16,5,0,1],[1,7,14,6,1,0],[0,0,0,0,0,0]],"t_rem":13,"t_wait":0,"rem_quantum":0},{"name":"C","t_arrival":8,"t_est":37,"state":3,"pages":3,"mem_data":[[1,9,12,2,0,0],[0,0,0,0,0,0],[1,13,21,7,0,1]],"t_rem":37,"t_wait":0,"rem_quantum":0}]

3

Answers


  1. you can do it by this way

    <p id="test" style="display:none"><?php echo json_encode($p_array); ?></p>
    
    <button onclick="actualizarProcesos(test)" id="">X</button>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    function actualizarProcesos(test){
    var p_array = $('#test').html();
    var p = JSON.parse(p_array);
    
    alert(p);
    

    }

    Login or Signup to reply.
  2. Tested working fine. Hope this help you.

        <?php 
         $p_array = [
           [
                 "name" => "A", 
                 "t_arrival" => 7, 
                 "t_est" => 25, 
                 "state" => 1, 
                 "pages" => 5, 
                 "mem_data" => [
                    [
                       1, 
                       8, 
                       13, 
                       5, 
                       0, 
                       1 
                    ], 
                    [
                          0, 
                          0, 
                          0, 
                          0, 
                          0, 
                          0 
                       ], 
                    [
                             1, 
                             11, 
                             17, 
                             3, 
                             1, 
                             1 
                          ], 
                    [
                                1, 
                                12, 
                                16, 
                                4, 
                                0, 
                                1 
                             ], 
                    [
                                   0, 
                                   0, 
                                   0, 
                                   0, 
                                   0, 
                                   0 
                                ] 
                 ], 
                 "t_rem" => 25, 
                 "t_wait" => 0, 
                 "rem_quantum" => 0 
              ], 
           [
                                      "name" => "B", 
                                      "t_arrival" => 6, 
                                      "t_est" => 13, 
                                      "state" => 2, 
                                      "pages" => 4, 
                                      "mem_data" => [
                                         [
                                            0, 
                                            0, 
                                            0, 
                                            0, 
                                            0, 
                                            0 
                                         ], 
                                         [
                                               1, 
                                               9, 
                                               16, 
                                               5, 
                                               0, 
                                               1 
                                            ], 
                                         [
                                                  1, 
                                                  7, 
                                                  14, 
                                                  6, 
                                                  1, 
                                                  0 
                                               ], 
                                         [
                                                     0, 
                                                     0, 
                                                     0, 
                                                     0, 
                                                     0, 
                                                     0 
                                                  ] 
                                      ], 
                                      "t_rem" => 13, 
                                      "t_wait" => 0, 
                                      "rem_quantum" => 0 
                                   ], 
           [
                                                        "name" => "C", 
                                                        "t_arrival" => 8, 
                                                        "t_est" => 37, 
                                                        "state" => 3, 
                                                        "pages" => 3, 
                                                        "mem_data" => [
                                                           [
                                                              1, 
                                                              9, 
                                                              12, 
                                                              2, 
                                                              0, 
                                                              0 
                                                           ], 
                                                           [
                                                                 0, 
                                                                 0, 
                                                                 0, 
                                                                 0, 
                                                                 0, 
                                                                 0 
                                                              ], 
                                                           [
                                                                    1, 
                                                                    13, 
                                                                    21, 
                                                                    7, 
                                                                    0, 
                                                                    1 
                                                                 ] 
                                                        ], 
                                                        "t_rem" => 37, 
                                                        "t_wait" => 0, 
                                                        "rem_quantum" => 0 
                                                     ] 
        ]; 
        $p_array = json_encode($p_array);
        ?>
        <button onclick='actualizarProcesos(<?php echo $p_array ?>)' >X</button>
        <script>
        function actualizarProcesos(arr){
            console.log(arr);
        }
    </script>
    
    Login or Signup to reply.
  3. I’ve made sure my json isnt sending any weird characters as it is mostly int except for the object attribute “name” which is string.

    Strings in JSON are delimited with " characters.

    <button onclick="actualizarProcesos(<?php echo json_encode($p_array)?>);">X</button>
    

    Your HTML attribute value is delimited with " characters.

    The " in the data will cut off the attribute value.

    You need to encode your data (using htmlspecialchars) for embedding in HTML:

    <?php echo htmlspecialchars(json_encode($p_array)); ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search