1

I am developing a simple web application. I have a MySQL database which stores a value. When I load my website, I want to run a php script that retrieves said value from the DB and passes it to javascript. I then use the javascript to either disable a button or not.

In my index.html:

<form action="someOtherScript.php" method="get">
    <input type="submit" name="button1" value="Run me now!">
</form>

<script>
    var value = "<?php echo $someVar; ?>";
    if(value == 0){
        document.getElementsByName("button1")[0].disabled = true;
    }
</script>

In my getValueFromDB.php:

<?php
    <!-- retrieve value from DB here (This part is not the problem)-->
    $someVar = 0;
?>

How can I tell my javascript in the index.html which php script to use (I have multiple)? So when the website loads I want my javascript to get the result from the getValueFromDB.php script.

Thanks a lot!

3 Answers3

2

You can try it with ajax, on page load

$( document ).ready(function() {
    $.ajax({
      url: "yourphpfile.php",
      success: function(response){
        if(result == 0)
        {
            document.getElementsByName("button1")[0].disabled = true; // instead of this maybe its better to use jquery - example below
            $('#button1').prop( "disabled", true );
        }
      }
    });
});
2

Try using the <?php include 'filepath' ; ?> includes reading documentation here, I think that is what you need

<form action="someOtherScript.php" method="get">
    <input type="submit" name="button1" value="Run me now!">
</form>

<script>
<?php include 'getValueFromDB.php' ; ?>
    var value = "<?php echo $someVar; ?>";
    if(value == 0){
        document.getElementsByName("button1")[0].disabled = true;
    }
</script>

You can not tell javascript how to use PHP, because JS is a client language and a PHP server language and the workflow is first PHP and second JS and not vice versa.

If you need to take php data with JS, you need to use AJAX

well (it's an example, not tested)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="someOtherScript.php" method="get">
    <input type="submit" name="button1" value="Run me now!">
</form>

<script>
    $.ajax({
     url: "getValueFromDB.php",
     success: function(result){
       if(result == 0){
        document.getElementsByName("button1")[0].disabled = true;
       }
    }});
</script>

php

 <?php
    <!-- retrieve value from DB here (This part is not the problem)-->
    $someVar = 0;
    echo $someVar
?>
eborrallo
  • 662
  • 7
  • 15
1

You would generally have your php script echo the values you want to get and preform an ajax call from your javascript to fetch the values, so

php:

<?php
<!-- retrieve value from DB here (This part is not the problem)-->
$someVar = 0;
echo $someVar;
?>

And js(you can put it directly in the html if you want).

<script>
    var dbData;
    var ajax = new XMLHttpRequest();
    ajax.open("GET", "PATH_TO_PHP_SCRIPT/getValueFromDB.php", true);
    ajax.send();
    ajax.onreadystatechange = function() {
         if (ajax.readyState == 4 && ajax.status == 200) {
           var data = ajax.responseText;
           dbData=data;
           //Deal with the response
    }
</script>

or, using jQuery:

<script>
     var dbData;
     $.ajax({
      url: "PATH_TO_YOUR_PHP_SCRIPT/getValueFromDB.php",
      method: "GET"
      }).done(function(d) {
          dbData=d;
          //Deal with the response
      });
</script>