Solved

Use js variable in PHP code

Posted on 2012-03-12
6
239 Views
Last Modified: 2012-03-24
I have a simple looping structure that compares two date fields like this:

eventdate = $('#billed').val();
<?php
$prior = 0; 
foreach($details as $billdate) {
	foreach($billdate as $k) { 
		if (strtotime($k['BILLED']) <= strtotime('2011-01-01')) {
			$prior += $pcvat_cat[$k['BILLED']][$k['CAT']];  
		}
	}
} 
?>

Open in new window


Currently, one of the two dates is hard-coded to '2011-01-01'.  What I would like to do is to replace the static date with the js variable 'eventdate' which appears above the PHP.

Is this possible?  How can I use the js date inside the PHP code?  Thanks.
0
Comment
Question by:DanielAttard
  • 3
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
Mark Brady earned 250 total points
ID: 37712842
PHP is not able to read any javascript variables as php is interpreted on and by the server before the web page is rendered and before any javascript is interpreted by the browser. You can use a php variable in javascript but not the other way around. Is it possible to create your array in javascript so you can use the other js variables? That might be a better solution if it is possible in your case.
0
 
LVL 5

Assisted Solution

by:Dobromir Yordanov
Dobromir Yordanov earned 250 total points
ID: 37712864
I'm backing up elvin66's suggestion to try to move the array to javascript but if that's completely impossible you could call a php file after a javascript has ran by calling the jquery ajax() function. I would however strongly recommend either moving the array to js or the generating the date variable you need in php itself. Don't go ajax unless you HAVE to
0
 

Author Comment

by:DanielAttard
ID: 37712888
Thanks for the comments.  You asked:

Is it possible to create your array in javascript so you can use the other js variables?

I'd like to do this, but don't know where to begin.  

How would I move the array to javascript?  Is this complicated?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 5

Expert Comment

by:Dobromir Yordanov
ID: 37712922
I don't think it would be that difficult if it's a static array but unfortunately it's not visible in the code snippet so I can't really tell you. Have you considered moving the event date to php instead? I believe it would be easier in your case. Where do you get that value from? Is it a static (as in hard-coded) or dynamic or user input? Unless it's chosen by the user, I'm pretty sure there is a very easy way to just move it to your php code (or at least copy its value). Notice that I'm not saying to get it from JavaScript to PHP but to get it to PHP way before it reaches JavaScript. Think if that's possible with your setup.
0
 

Author Comment

by:DanielAttard
ID: 37712954
Thanks again cp.  

Agreed, I would prefer to move the event date to php, but how to accomplish this?  Currently, the event date is set in javascript with this:

eventdate = $('#billed').val();

Open in new window


which is a reference to the date value contained in the following div:

   
<div>
        <input type="text" id="billed" name="billed" value="<?php echo date('Y-m-d'); ?>" readonly >
    </div>

Open in new window


This date value can be manipulated by the user.  

I thought about your question, whether it is possible to "get the date value to PHP way before it reaches JavaScript", and I don't think that this is going to be possible.  The date changes from time to time by the user, so I need to be able to pass that date value into my PHP at different times with different values.  Not sure what to try next . . .
xx
0
 
LVL 5

Expert Comment

by:Dobromir Yordanov
ID: 37712993
Well that doesn't work so it's time to try to move the array and loop to JavaScript for which we would need to take a look at a bigger chunk of your PHP code. If that also fails you have only one solution - refreshing page or asynchronous request, i.e. Ajax. I'll try to help you tomorrow but I don't know if elvin66 or someone else wouldn't like to help you meanwhile... I'll check back and help you if still needed. Take care!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML5 frame 5 25
Jquery keyup 4 17
Button and js nou working 3 6
What web tools we can use to create a web apps that holds clients data 3 10
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now