Javascript ´variables to control PHP databank search












0















I currently have an SQL database that I would like to essentially live search by means of Javascript. I'm feeling a bit lost in how to implement the following:



JAVASCRIPT/HTML (index.php):



<form>
Centre (cents):<br>
<input type="number" id="centreCents" value="0"><br>
Range (cents):<br>
<input type="number" id="rangeCents" value="0">
</form>

<button onclick="myFunction()">Search</button>

<table>
<tr>
<th>ID</th>
<th>CENTS</th>
<th>HD</th>

</tr>
<tbody id="data">

</tbody>
</table>

<script>

function myFunction(){
// call ajax
var ajax = new XMLHttpRequest();
var method = "GET";
var url = "data.php";
var asynchronous = true;

ajax.open(method, url, asynchronous);

// sending request
ajax.send();

// receiving response from data.php
ajax.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200){
// converting JSON back to array
var data = JSON.parse(this.responseText);
console.log(data); // for debugging

// html value for <tbody>
var html = "";
// loop through the data
for (var a = 0; a < data.length; a++){
var ID = data[a].id;
var cents = data[a].cents;
var HD = data[a].hd;

// storing in html

html += "<tr>";
html += "<td>" + ID + "</td>";
html += "<td>" + cents + "</td>";
html += "<td>" + HD + "</td>";
html += "</tr>"
}
// replacing the body of <tbody> of <table>
document.getElementById("data").innerHTML = html;

}
}
}
</script>


PHP (data.php):



<?php

//getting data from databank

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "enharmonics";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

// getting data from table

$centSearch = "SELECT * FROM enharmonictable WHERE cents>=385 AND cents<=388 ORDER BY hd ASC;";
$result = mysqli_query($conn, $centSearch);

// storing in array

$data = array();
$HDdata = array();
while ($row = mysqli_fetch_assoc($result)) {
$data = $row;
}

// returning response in JSON format

echo json_encode($data);


Ideally, the numerical values of $centSearch would be determined by the number boxes in the html as centreCents - rangeCents and centreCents + rangeCents. Then everything would run (update) when the button is pushed again. Looking for some guidance - really hoping it is possible! Everything has been smooth sailing so far.










share|improve this question



























    0















    I currently have an SQL database that I would like to essentially live search by means of Javascript. I'm feeling a bit lost in how to implement the following:



    JAVASCRIPT/HTML (index.php):



    <form>
    Centre (cents):<br>
    <input type="number" id="centreCents" value="0"><br>
    Range (cents):<br>
    <input type="number" id="rangeCents" value="0">
    </form>

    <button onclick="myFunction()">Search</button>

    <table>
    <tr>
    <th>ID</th>
    <th>CENTS</th>
    <th>HD</th>

    </tr>
    <tbody id="data">

    </tbody>
    </table>

    <script>

    function myFunction(){
    // call ajax
    var ajax = new XMLHttpRequest();
    var method = "GET";
    var url = "data.php";
    var asynchronous = true;

    ajax.open(method, url, asynchronous);

    // sending request
    ajax.send();

    // receiving response from data.php
    ajax.onreadystatechange = function()
    {
    if (this.readyState == 4 && this.status == 200){
    // converting JSON back to array
    var data = JSON.parse(this.responseText);
    console.log(data); // for debugging

    // html value for <tbody>
    var html = "";
    // loop through the data
    for (var a = 0; a < data.length; a++){
    var ID = data[a].id;
    var cents = data[a].cents;
    var HD = data[a].hd;

    // storing in html

    html += "<tr>";
    html += "<td>" + ID + "</td>";
    html += "<td>" + cents + "</td>";
    html += "<td>" + HD + "</td>";
    html += "</tr>"
    }
    // replacing the body of <tbody> of <table>
    document.getElementById("data").innerHTML = html;

    }
    }
    }
    </script>


    PHP (data.php):



    <?php

    //getting data from databank

    $dbServername = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "enharmonics";

    $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

    // getting data from table

    $centSearch = "SELECT * FROM enharmonictable WHERE cents>=385 AND cents<=388 ORDER BY hd ASC;";
    $result = mysqli_query($conn, $centSearch);

    // storing in array

    $data = array();
    $HDdata = array();
    while ($row = mysqli_fetch_assoc($result)) {
    $data = $row;
    }

    // returning response in JSON format

    echo json_encode($data);


    Ideally, the numerical values of $centSearch would be determined by the number boxes in the html as centreCents - rangeCents and centreCents + rangeCents. Then everything would run (update) when the button is pushed again. Looking for some guidance - really hoping it is possible! Everything has been smooth sailing so far.










    share|improve this question

























      0












      0








      0








      I currently have an SQL database that I would like to essentially live search by means of Javascript. I'm feeling a bit lost in how to implement the following:



      JAVASCRIPT/HTML (index.php):



      <form>
      Centre (cents):<br>
      <input type="number" id="centreCents" value="0"><br>
      Range (cents):<br>
      <input type="number" id="rangeCents" value="0">
      </form>

      <button onclick="myFunction()">Search</button>

      <table>
      <tr>
      <th>ID</th>
      <th>CENTS</th>
      <th>HD</th>

      </tr>
      <tbody id="data">

      </tbody>
      </table>

      <script>

      function myFunction(){
      // call ajax
      var ajax = new XMLHttpRequest();
      var method = "GET";
      var url = "data.php";
      var asynchronous = true;

      ajax.open(method, url, asynchronous);

      // sending request
      ajax.send();

      // receiving response from data.php
      ajax.onreadystatechange = function()
      {
      if (this.readyState == 4 && this.status == 200){
      // converting JSON back to array
      var data = JSON.parse(this.responseText);
      console.log(data); // for debugging

      // html value for <tbody>
      var html = "";
      // loop through the data
      for (var a = 0; a < data.length; a++){
      var ID = data[a].id;
      var cents = data[a].cents;
      var HD = data[a].hd;

      // storing in html

      html += "<tr>";
      html += "<td>" + ID + "</td>";
      html += "<td>" + cents + "</td>";
      html += "<td>" + HD + "</td>";
      html += "</tr>"
      }
      // replacing the body of <tbody> of <table>
      document.getElementById("data").innerHTML = html;

      }
      }
      }
      </script>


      PHP (data.php):



      <?php

      //getting data from databank

      $dbServername = "localhost";
      $dbUsername = "root";
      $dbPassword = "";
      $dbName = "enharmonics";

      $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

      // getting data from table

      $centSearch = "SELECT * FROM enharmonictable WHERE cents>=385 AND cents<=388 ORDER BY hd ASC;";
      $result = mysqli_query($conn, $centSearch);

      // storing in array

      $data = array();
      $HDdata = array();
      while ($row = mysqli_fetch_assoc($result)) {
      $data = $row;
      }

      // returning response in JSON format

      echo json_encode($data);


      Ideally, the numerical values of $centSearch would be determined by the number boxes in the html as centreCents - rangeCents and centreCents + rangeCents. Then everything would run (update) when the button is pushed again. Looking for some guidance - really hoping it is possible! Everything has been smooth sailing so far.










      share|improve this question














      I currently have an SQL database that I would like to essentially live search by means of Javascript. I'm feeling a bit lost in how to implement the following:



      JAVASCRIPT/HTML (index.php):



      <form>
      Centre (cents):<br>
      <input type="number" id="centreCents" value="0"><br>
      Range (cents):<br>
      <input type="number" id="rangeCents" value="0">
      </form>

      <button onclick="myFunction()">Search</button>

      <table>
      <tr>
      <th>ID</th>
      <th>CENTS</th>
      <th>HD</th>

      </tr>
      <tbody id="data">

      </tbody>
      </table>

      <script>

      function myFunction(){
      // call ajax
      var ajax = new XMLHttpRequest();
      var method = "GET";
      var url = "data.php";
      var asynchronous = true;

      ajax.open(method, url, asynchronous);

      // sending request
      ajax.send();

      // receiving response from data.php
      ajax.onreadystatechange = function()
      {
      if (this.readyState == 4 && this.status == 200){
      // converting JSON back to array
      var data = JSON.parse(this.responseText);
      console.log(data); // for debugging

      // html value for <tbody>
      var html = "";
      // loop through the data
      for (var a = 0; a < data.length; a++){
      var ID = data[a].id;
      var cents = data[a].cents;
      var HD = data[a].hd;

      // storing in html

      html += "<tr>";
      html += "<td>" + ID + "</td>";
      html += "<td>" + cents + "</td>";
      html += "<td>" + HD + "</td>";
      html += "</tr>"
      }
      // replacing the body of <tbody> of <table>
      document.getElementById("data").innerHTML = html;

      }
      }
      }
      </script>


      PHP (data.php):



      <?php

      //getting data from databank

      $dbServername = "localhost";
      $dbUsername = "root";
      $dbPassword = "";
      $dbName = "enharmonics";

      $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

      // getting data from table

      $centSearch = "SELECT * FROM enharmonictable WHERE cents>=385 AND cents<=388 ORDER BY hd ASC;";
      $result = mysqli_query($conn, $centSearch);

      // storing in array

      $data = array();
      $HDdata = array();
      while ($row = mysqli_fetch_assoc($result)) {
      $data = $row;
      }

      // returning response in JSON format

      echo json_encode($data);


      Ideally, the numerical values of $centSearch would be determined by the number boxes in the html as centreCents - rangeCents and centreCents + rangeCents. Then everything would run (update) when the button is pushed again. Looking for some guidance - really hoping it is possible! Everything has been smooth sailing so far.







      javascript php html database livesearch






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 11:50









      Thomas NicholsonThomas Nicholson

      183




      183
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318853%2fjavascript-%25c2%25b4variables-to-control-php-databank-search%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318853%2fjavascript-%25c2%25b4variables-to-control-php-databank-search%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine