在PHP函数上使用Javascript数组,无论它在同一个文件上还是在另一个文件上以及其他函数上 [英] Use Javascript array on PHP function be it on the same file or on another along with other functions

查看:87
本文介绍了在PHP函数上使用Javascript数组,无论它在同一个文件上还是在另一个文件上以及其他函数上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过多次了,但是我仍然没有找到我所面临的具体问题的答案.我正在创建一个网页,每当用户单击特定按钮时,我都希望该页面从php函数上动态创建的表中获取数据.由于PHP没有内置函数(至少据我所知),因此我决定使用Javascript将表数据转换为带有JSON元素的数组.我想通过使用以下方法来做到这一点:

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);
        }

我正在另一个函数中使用此数据将其解析为PHP函数:

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");
            }
        }

我知道上面的方法不是正确的方法,我也对AJAX有所了解.我的问题是:有没有办法可以在同一文件上调用函数?如果没有,是否有更简便的方法在另一个具有我所需功能的PHP文件上使用AJAX?

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?

谢谢大家!

推荐答案

问题的答案是:不.您不能在PHP中使用Javascript中的变量.您可以使用PHP生成有效的Javascript代码,但反之则不行.原因是PHP在服务器端处理,而Javascript在客户端(浏览器)处理

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)

注意:您也可以使用Node在服务器端处理Javascript,但是您已经在使用PHP.如何将服务器端javascript与NodeJ一起使用不在问题的范围之内.

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.

现在,您可以使用PHP创建该表,而完全不使用JS.我假设您的数据为数组格式,因此请查看像foreach这样的PHP循环.然后通过回显(请参见echo(''))表标签(table,thead,tbody,td,tr等)来构建表.

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).

这篇关于在PHP函数上使用Javascript数组,无论它在同一个文件上还是在另一个文件上以及其他函数上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆