1

I want to print a button value come from PHP fetch but every time the value came with space before the text value for example " ON" and I want it "ON"

My code in php file:

  <?php
  $DATABASE_HOST = 'ssite.com';
  $DATABASE_USER = 'user';
  $DATABASE_PASS = 'pass';
  $DATABASE_NAME = 'db';
  // Try and connect using the info above.
  $db = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS,             
   $DATABASE_NAME);
  if (!$db){

die("Connection Failed: ". mysqli_connect_error());

  }

    $db_select = "SELECT * FROM led WHERE id = 1";

     $result = mysqli_query($db, $db_select);
  if($result->num_rows == 1){
   while($row = mysqli_fetch_assoc($result)){
   echo($row['status']);
           }   
       }

     ?>

In HTML for button:

   <input type="button" id="ledonof" onclick="myFunction()" value="<?php 
   include ('led.php'); ?>">

my issue is here with the value output is come like this : " ON" I don't know why the space is coming before the value?

Javascript file:

    function myFunction(){
    var ledactual=document.getElementById("ledonof").value 
    var ledon="ON"
    var ledoff="OFF"
    if (ledactual == ledoff){
    $.get("led.php", function(led) {
      $("#ledonof").prop('value',led);
    })}
if (ledactual == ledon){

    $.get("ledoff.php", function(ledoff) {
      $("#ledonof").prop('value',ledoff);
    })}

    };
M.Saeed
  • 303
  • 4
  • 14

3 Answers3

1

Just for debug:

<input type="button" id="ledonof" onclick="myFunction()" value="<?php trim(include ('led.php')) ?>">
GabrieleMartini
  • 1,665
  • 2
  • 19
  • 26
1

Please check the view-source and then clean the code.
Note: remove the inline onclick

$("#ledonof").on("click",function(e) {
  e.preventDefault();
  var ledactual = $.trim(this.value).toUpperCase();
  if (["ON", "OFF"].indexOf(ledactual) == -1) {
    console.log("Wrong value", ledactual); //debug
    return;
  }
  var php = "led" + (ledactual == "OFF" ? "off" : "") + ".php";
  $.get(php, function(led) {
    $("#ledonof").val(led);
  })
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-1

check your database values and use the PHP trim method on the value trim(include ('led.php')

  • You can't `trim` an `include` call. Its return value is [either `FALSE` or `1`](https://www.php.net/manual/en/function.include.php). – ceejayoz May 20 '19 at 21:39