0

I was wondering if there was any possible way to keep track of JavaScript variables within a for loop inside PHP.

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. The front end is designed whilst using a lot of JavaScript. I'm going to need to populate JavaScript fields with fields from my PHP object through multiple iterations. Hence, the PHP inside the JavaScript for loop.

Neither the front end nor the back end is written by me, mind you. :P

0

4 Answers 4

1

You fundamentally misunderstand how PHP and Javascript work. PHP is executed on the server, while the page is being generated, BEFORE the page is sent to the client.

Javascript kicks in afterwards, after the page has been generated and received by the client.

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.

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

4 Comments

Yeah, I understand this fundamental difference here. I just hoped there would be some possible way..
Sure, you can use AJAX to round-trip the JS-side value of i through PHP, but what would be the point?
Point is, I'm trying to combine a hack job written in a different language with different design and combine it with another hack job that wasn't ever meant to handle it. I can either rewrite either end to fit with each other, or write more hacky job to try to mediate the two. The second option is the least expensive right now so as long as it gets the job done.. haha. Thanks for the idea though. that's probably the route I'll go.
So the PHP stuff sends a PHP array to Javascript? You mean literal serialize() output? or json_encode() output? If it's json, then it's already native javascript and there's no need to get PHP involved again.
0

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.

Example

Imagine this is a HTML view...

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

...the output would be...

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

1 Comment

Yeah, I definitely know it won't work and I definitely know it's not proper PHP syntax. I was just wondering if there was any possible way to achieve this.
0

You cant mix server side scripting and client side scripting.

For this purpose only AJAX is made. :)

You can go with jquery, mootool, closure... etc

Comments

0

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.

This post talks about it: http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/290401

If I'm on the wrong track, then have a read anyway :)

Comments

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.