简体   繁体   中英

Use Javascript array on PHP function be it on the same file or on another along with other functions

I know this question has been asked multiple times but I still have not found an answer to the specific problem I am facing. I am creating a web page and whenever a user clicks a specific button, I want the page to get data from a dynamically created table on a php function. Since PHP doesn't have a built-in function to do so, at least not to my knowledge, I have decided to use Javascript to convert the table data into am array with JSON elements. I figured out to do that by using:

function getTableData(){
            var array = [];
            var headers = [];
            $('#tablaListado th').each(function(index, item) {
                headers[index] = $(item).html();
            });
            $('#tablaListado tr').has('td').each(function() {
                var arrayItem = {};
                $('td', $(this)).each(function(index, item) {
                    arrayItem[headers[index]] = $(item).html();
                });
                array.push(arrayItem);
            });

            actualizarCostos(array);
        }

I am using this data in another function to parse it on to a PHP function:

function actualizarCostos(array){
            if(array.constructor === Array){
                for(var i = 0; i < array.length ; i++){
                    <?php updateCosts(JSON.stringify(array[i]))?>
                }
            }else{
                alert("Input must be array");
            }
        }

I know the way above is not the correct way of doing so, I have also read up on AJAX a little bit. My question is: is there a way I can call a function on the same file? If not, is there an easier way to use AJAX on another PHP file which will have the function I need along with other ones?

Thank you all!

The answer to the question is: No. You can't use a variable from Javascript inside PHP. You can generate valid Javascript code with PHP, but not the other way around. The reason is that PHP is processed in the server side, while Javascript is processed on the client side (the browser)

NOTE : You can process Javascript on the server side as well using Node, but you are already using PHP. How to use server side javascript with NodeJs is out of the scope of the question.

Now, you can create that table with PHP, and not use JS at all. I'm assuming your data is in an array format, so look into PHP loops like foreach . Then build a table by echoing (see echo('') ) the table tags (table, thead, tbody, td, tr, etc).

You can't use a variable from Javascript inside PHP

because

Javascript (Client side) and PHP (Server Side)

So in current context what you can do is as follows

Client Side

function actualizarCostos(array){
            if(array.constructor === Array){
               // Here you need ajax

              $.post('http://myhost.mydomain.com/update_cost.php',
                     { table_data : array }, function(response){
                              // do something after getting response
                   });
            }else{
                alert("Input must be array");
            }
        }

Server Side (update_cost.php)

<?php
        function updateCosts($item)
        {
             // do something here...
        }

        if(isset($_POST['table_data']))
        {
               foreach($_POST['table_data'] as $index => $item)
               {
                      // do something here 
                      updateCosts($item);
               }

               // Response back - if needed
               echo json_encode(array('status'=>true, 'msg'=>'Some Message'));
        }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM