简体   繁体   English

在Javascript循环内运行PHP,有什么方法可以跟踪迭代?

[英]Running PHP inside of a Javascript loop, any way to keep track of iterations?

I was wondering if there was any possible way to keep track of JavaScript variables within a for loop inside PHP. 我想知道是否有任何可能的方法来跟踪PHP内部for循环中的JavaScript变量。

I definitely know that this is not good design. 我绝对知道这不是一个好的设计。 However, I'm trying to hack some existing code. 但是,我正在尝试破解一些现有代码。 If I am able to make this work, I may not have to dump the existing code... 如果我能够完成这项工作,则可能不必转储现有代码...

Thanks you guys. 谢谢你们 Any hacks, tips, tricks will work!! 任何技巧,技巧和窍门都可以!!

Example: 例:

 //JavaScript for loop for (var i = 0; i <4; ++i) { //Echo the JavaScript variable "i", the iteration of the loop <?php echo /*JavaScript var i */ ; ?> 

} }

EDIT: More background info 编辑:更多背景信息

I'm working on a project where the data is supplied through XML and the API is written in PHP and returns an PHP array. 我正在一个项目中,数据通过XML提供,API用PHP编写并返回PHP数组。 The front end is designed whilst using a lot of JavaScript. 在使用大量JavaScript的同时设计了前端。 I'm going to need to populate JavaScript fields with fields from my PHP object through multiple iterations. 我将需要通过多次迭代用来自我的PHP对象的字段填充JavaScript字段。 Hence, the PHP inside the JavaScript for loop. 因此,JavaScript for循环内的PHP。

Neither the front end nor the back end is written by me, mind you. 请注意,前端和后端都不是我写的。 :P :P

You fundamentally misunderstand how PHP and Javascript work. 您从根本上误解了PHP和Javascript的工作方式。 PHP is executed on the server, while the page is being generated, BEFORE the page is sent to the client. 在页面生成过程中,在页面发送到客户端之前,PHP在服务器上执行。

Javascript kicks in afterwards, after the page has been generated and received by the client. 在客户端生成并接收页面之后,Javascript将随后启动。

What you're trying to do is getting a driver to change the radio station in a car while the car's still on the assembly line in the factory. 您要做的就是让司机在汽车仍在工厂装配线上的同时更换汽车中的广播电台。 Not possible. 不可能。

That will definitely not work. 那绝对是行不通的。

The JavaScript loop's body will be whatever PHP echoes, and in your example, that is not valid PHP syntax. JavaScript的循环体将是什么PHP相呼应,并在你的榜样,是无效的PHP语法。

Example

Imagine this is a HTML view... 想象这是一个HTML视图...

for (var i = 0; i < 4; ++i) {
    <?php echo 'a';   ?>
}

...the output would be... ...输出将是...

for (var i = 0; i < 4; ++i) {
    a
}

You cant mix server side scripting and client side scripting. 您不能混合使用服务器端脚本和客户端脚本。

For this purpose only AJAX is made. 为此,仅制作AJAX。 :) :)

You can go with jquery, mootool, closure... etc 您可以使用jquery,mootool,closure ...等

I think I understand what you are trying to do. 我想我知道您要做什么。 If on a basic level, you want to use the value from a PHP variable within the javascript on your client side, they you need to echo out a whole section and 'print' the php value and assign it to a javascript variable. 如果在基本级别上,您想使用客户端javascript中PHP变量中的值,则需要回显整个部分并“打印” php值并将其分配给javascript变量。

This post talks about it: http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/290401 这篇文章讨论了它: http : //www.daniweb.com/web-development/javascript-dhtml-ajax/threads/290401

If I'm on the wrong track, then have a read anyway :) 如果我走错了方向,那么还是请阅读:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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