0

I have a php variable and i use it in a javascript function. I get the result on chrome, but the variable was undefined on FF and IE. The alert() give: NaN.

<?php

require('connection.php');
$mod=$_POST['mod'];
$req=$bdd->prepare('SELECT * FROM theo WHERE affaire=?');
$req->execute(array($mod));
$tableau=$req->fetchAll(PDO::FETCH_ASSOC);

//var_dump($tableau);
foreach ($tableau as $key => $t)
{
  $date_theo_cre=$t['date_theo_cre'];
  $date_theo_fin=$t['date_theo_fin'];

}
$req->closeCursor();
?>

<script>
$(function(){
// Initialisation des champs
$('#dateJour').val(convertDateToString(new Date()));

});
var minDate = new Date(Date.parse("<?php echo  $date_theo_cre;?>;"));
var today = new Date();
var maxDate = new Date(Date.parse("<?php echo  $date_theo_fin;?>;"));
// Mise à jour de l'avancement

  var nbJoursTotal = Math.floor((maxDate.getTime() - minDate.getTime()) / 
 86400000);
  var nbJoursPasses = Math.floor((today.getTime() - minDate.getTime()) / 
 86400000);

  var pourcentage = nbJoursPasses / nbJoursTotal * 100;

  // On gère les cas limites
  if (pourcentage < 0) {
    pourcentage = 0;
  } else if (pourcentage > 100) {
    pourcentage = 100;
  }

  $("#avancement").reportprogress(pourcentage);

</script>

The call to the method: $("#avancement").reportprogress(pourcentage); give the result of the progress in an HTML div on chrome but nothing on FF and IE.

Can anyone please help me out?

Thanks

6
  • 4
    So what does the code look like after the PHP has executed? Hard to debug without knowiing what the PHP is spitting out and convertDateToString is doing. Commented Jun 26, 2017 at 12:51
  • 1
    My guess is you are passing invalid date string format to date object. Chrome is more forgiving about parsing invalid dates than other browsers Commented Jun 26, 2017 at 12:53
  • try to add a console.log minDate, maxDate, minDate.getTime() and maxDate.getTime() to see if your values are correct before calculations, chalietfl is probably right Commented Jun 26, 2017 at 12:56
  • If you're getting an error in JavaScript, the first thing you should be looking at is your actual resulting JavaScript code. It seems reasonable that your PHP code isn't outputting what you assume it is, so what is it outputting? Commented Jun 26, 2017 at 12:56
  • 1
    Date.parse("<?php echo $date_theo_cre;?>;") extra colon is here. Remove it and check it once. Commented Jun 26, 2017 at 12:56

1 Answer 1

3

Can you try removing the unwanted semi-colon from the php statement like this:

var minDate = new Date(Date.parse("<?php echo  $date_theo_cre;?>"));
var today = new Date();
var maxDate = new Date(Date.parse("<?php echo  $date_theo_fin;?>"));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks to all of you.
Great. It will be good if you mark this as answer for other users.

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.