0

Having a bad day. This one has stumped me all morning. All the solutions I've found have stopped one step short of where I need to go.

I have a legacy PHP/JS app that I'm working on. Rather than trying to explain it, I'll just show what I need to do.

<?php
$phpDate_1 = new Date($someDate);
$phpDate_2 = new Date($someOtherDate);
//...There are a bunch of these
$phpDate_n = new Date($endOfTime);

<script language="javascript">
function myFunction() {
    var line = aUserSelection; //an int from user which tells me what date to use
    //Next line is the problem. I'm trying to pull the month from the appropriate PHP date into the JS variable.
    var theMonth = "<?php echo $phpDate_" + line + "->getMonth();?>";
}
</script>
?>

I must have tried 20-30 combinations of single and double quotes, escapes, dots, pluses, and so on, but I keep getting errors over the "line" part. Unexpected character, encapsed strings, etc.

Hoping someone can point me in the right direction because my brain is fried at this point. Answers in pure JS and PHP only please because that's how the app is built. Thanks.

mckinnej
  • 33
  • 4
  • 2
    You can’t use JavaScript to build up a PHP variable name. PHP is parsed well before any JavaScript is interpreted. – Martin Bean May 31 '18 at 15:41
  • you cannot mix php and javascript this way...take a look at the possible dublicate question/answer. you would need to either get the month from the server with an ajax call or store the line -> month info into a js data structure – cypherabe May 31 '18 at 15:43
  • It is, because PHP is processed before the page is sent to the server, javascript after. PHP cannot access variables created by javascript, and javascript cannot call PHP functions. In order for PHP and javascript to interact, ajax calls are needed. – aynber May 31 '18 at 15:45
  • @aynber You're right; sorry I was focused on the OP's error, and didn't look too closely at the code. I've removed my incorrect comments to keep things clean. – RToyo May 31 '18 at 15:52
  • 1
    I would probably iterate over all the "$phpDate_x" variables with something like var availableDates = ".json_encode($vals).""; ?> and then in the JS code you can do something along the lines of theMonth = availableDates[line]; – cjs1978 May 31 '18 at 15:57
  • DOH! The syntax errors were distracting me from the real problem. Told you it's a bad day. :) Thanks for setting me straight! – mckinnej May 31 '18 at 16:22

1 Answers1

0

You need to close your php (?>) before outputting the javascript to fix the syntax error that you got.

However, with that said, you are trying to incorporate the javascript line variable into the variable name for $phpDate, to generate something like $phpDate_1.

If you don't want to go with an AJAX solution, your best bet would be to output each line's date into a javascript array. This is strongly discouraged, but if this is a legacy application that you cannot make many changes to, this might be your only option.

RToyo
  • 2,877
  • 1
  • 15
  • 22
  • 1
    Not even remotely close to an answer – Vivick May 31 '18 at 15:39
  • @Vivick Why not? The OP described the error, and this identifies and resolves the problem. – RToyo May 31 '18 at 15:40
  • 2
    Fairly simple, in one expression : `"getMonth();?>";` is still not what the OP intended to do with it – Vivick May 31 '18 at 15:43
  • @Vivick Thank you for pointing that out. I looked at the OP's error; then glanced at their code to try find the error, and glazed over the ` – RToyo May 31 '18 at 15:51