0

Hi I'm trying to update my HTML reservation sheet based on a MySQL query. I tried to use a PHP while loop in my JavaScript but I can't do it.
Here's my code

<script type="text/javascript">
<?php
$date = $_GET['JOUR'];
$login = $_GET['NAME'];
require('bdd.php');
//here my query
$req = "SELECT * FROM RESERVATION WHERE resa_jour = '$date'";
//here I exec the query
$reponse = $bdd->query($req);
while ($data = $reponse->fetch())
{
    ?>
        jQuery(function ($) {
            if (<?php echo $data['id'] ?>) {
                $('.<?php echo $data['resa_salle'] . $data['resa_periode'] ?>').addClass('disable');
            }
        });
<?php } ?>
</script>
Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
Pierre C.
  • 23
  • 2
  • 1
    Please ask a specific, code-based question. "I can't do it" doesn't really tell us what the problem is. – Mitya Jan 20 '17 at 10:55
  • 1
    Can you provide the result html? Try to intertwine less between php and js, it's hard to read, and it'll make it hard to debug, and it'll create a lot of bugs because of that. – Swimburger Jan 20 '17 at 10:56
  • Alot of misunderstanding's going on here. First, your question should be more specified. And the second... I don's see any reason to use loop here.. you can just display that jQuery if your results are correct. And also ... `if ()` will be for example `if (15)` after server side scripts are executed (`if(15)` == always true). – Dawid Zbiński Jan 20 '17 at 10:58
  • 1
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 20 '17 at 10:59
  • It is generally best practice to minimise the intermixing of languages like this, because it can be really hard to read and debug and becomes a maintenance headache. Move as much of the PHP code out of the JS as possible; generate a JSON string with it, and then use that JSON as an object in your JS code. That'll mean you only have one line of PHP in the JS code, and the rest can be debugged as pure JavaScript. Much much easier. – Simba Jan 20 '17 at 11:00
  • Sorry it's my first time doing javascript and posting on stackoverflow, I have an Fatal error: Uncaught Error: Call to a member function fetch() on boolean – Pierre C. Jan 20 '17 at 11:01

1 Answers1

1

I think this is the bad idea. You should pass only result in a JS variable and then first saw that in which format that is JSON or ARRAY. Then run a loop in as

$.each(data, function(index, value){
-- your code here -- 
});

JS and add class in that way.

Rana Ghosh
  • 4,514
  • 5
  • 23
  • 40