1

I'm trying to write some input in an HTML element and get the input text using JS, and after that send the value to another file using AJAX, but it's not working and i get only "null" as result

is there a reason?

this is my HTML

<button type="button" onclick="loadDoc()">Value</button>
     <input type="number" class="test" id="test" name="test" value="" style="width:10%" >
     <div id="mostraAttuale"></div>
    </br>

this is my JS function

function loadDoc() {
    var value = document.getElementById("test");
    alert(value);
    var url = <?php echo json_encode($test); ?>;
    var email = <?php echo json_encode($mail); ?>;
    var valuereq = new XMLHttpRequest();
    valuereq.open("GET", url+'?email='+email+'&value='+value, true);
    valuereq.send();
    valuereq.status;
    valuereq.onreadystatechange = function () {
        if (valuereq.readyState == 4 && valuereq.status == 200) {
            var return_data = valuereq.responseText;
            document.getElementById("mostraAttuale").innerHTML = "ultimo valore inserito: " + return_data;
        }
        else document.getElementById("mostraAttuale").innerHTML = valuereq.status;
    }
    document.getElementById("test").value="";
}

Thank you in advance.

Filippo
  • 97
  • 1
  • 11

2 Answers2

3

Try this:

function loadDoc() {
    var value = document.getElementById("test").value;
    alert(value);
    var url = "<?=json_encode($test) ?>";
    var email = "<?=json_encode($mail) ?>";
    var valuereq = new XMLHttpRequest();
    valuereq.open("GET", url+'?email='+email+'&value='+value, true);
    valuereq.send();
    valuereq.status;
    valuereq.onreadystatechange = function () {
      if (valuereq.readyState == 4 && valuereq.status == 200) {
          var return_data = valuereq.responseText;
          document.getElementById("mostraAttuale").innerHTML = "ultimo valore inserito: " + return_data;
      }
      else 
         document.getElementById("mostraAttuale").innerHTML = valuereq.status;
    }
    document.getElementById("test").value="";
}
mozkomor05
  • 1,367
  • 11
  • 21
  • I think you should remove double quotes from `url` and `email` declaration, do `var url = =json_encode($test) ?>;` or var `url = "= $test ?>";` but in both cases you should never mix php code and js code – YouneL Jan 06 '18 at 23:35
  • But [json_encode](http://php.net/manual/en/function.json-encode.php) returns string and without double quotes output will be: `var url = some string without quotes...` – mozkomor05 Jan 06 '18 at 23:42
  • It returns string with double quotes, try it [here](http://sandbox.onlinephpfunctions.com/code/1b422ed525a8051fbd350c10ab4fd459a4f243d1) – YouneL Jan 06 '18 at 23:46
  • But if he encode array it returrns without double quotes: https://ideone.com/7f1Ypv – mozkomor05 Jan 06 '18 at 23:53
  • thank you very much for your help this was the solution. – Filippo Jan 07 '18 at 13:23
2

The line var value = document.getElementById("test"); doesn't get the value but the "HTMLInputElement". Replace it by :

 var value = document.getElementById("test").value;
Arkerone
  • 1,971
  • 2
  • 22
  • 35
  • But that is not all. He has an error in putting php into js: https://stackoverflow.com/questions/3345457/how-to-put-php-inside-javascript – mozkomor05 Jan 06 '18 at 23:29