-2

I get a error when I use to Php in Javascript. I check a console and Error name is "Uncaught SyntaxError: Invalid or unexpected token". How I solve this error

My code is;

var data = ["<?php
  foreach ($db->query("SELECT * FROM ev_uygunluk where evid='{$evid}' order by id ASC") as $listele) 
{
echo"
{date: ".$listele['tarih'].", value: 'rezerve'}";

}

  ?>"];

Console error line is; consol error line

leatred
  • 1
  • 2
  • I try add but again ı get a error. Where should I add in codes – leatred Feb 29 '20 at 12:34
  • 1
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Dave Feb 29 '20 at 12:42

3 Answers3

0

Comma (s) is missing

var data = [ <?php
  foreach ($db->query("SELECT * FROM ev_uygunluk where evid='{$evid}' order by id ASC") as $listele) 
{
echo "{date: new Date('".$listele['tarih']."'), value: 'rezerve'}, ";  // this comma !
}
  ?> ];
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
0

You're having double quotes wrongly placed inside your brackets and your objects aren't separated correctly with commas. Try this instead:

<?php
$tmp = [];
foreach($db->query("SELECT * FROM ev_uygunluk where evid='{$evid}' order by id ASC") as $listele) {
    $tmp[] = "{date: '{$listele['tarih']}', value: 'rezerve'}";
}


echo "var data = [".implode(', ', $tmp)."]";
Repox
  • 15,015
  • 8
  • 54
  • 79
0

you should have use es6 back tick `` to avoid this type of mistake

var data = [<?php foreach ($db->query("SELECT * FROM ev_uygunluk where evid='{$evid}' order by id ASC") as $listele) { echo" {date: ".$listele['tarih'].", value: 'rezerve'}"; } ?>];

I have used backtick but code editor not displaying it use before <? and at after the end ?> Hope this will solve your issue.

zaheer ahmed
  • 69
  • 1
  • 5