1

I have tried to pass on the text from a php page into my html page, as described by Chris Bakers answer (javascript, not jquery). Call php function from javascript

The code works, if i use the normal text (id=output), but i would like to output the text to a textarea (id=text1) instead of a normal text, just changing the id does not work.

This is my code:

<html>
 <head>
 </head>
 <body>
 <textarea id="text1" style="background-color: #969696" cols="50" rows="10" readonly></textarea>
 <div id="output">waiting for action</div>
 </body>
 <script>


    function getOutput() {
    var value = document.getElementById("artikelnr").value;
    var file = selectedValue()+".csv";
      getRequest(
          "verarbeitung.php?eingabe="+value+"&eingabe2="+file, // URL for the PHP file
           drawOutput,  // handle successful request
           drawError    // handle error
      );
      return false;
    }  
    // handles drawing an error message
    function drawError() {
        var container = document.getElementById('text1');
        container.innerHTML = 'Bummer: there was an error!';
    }
    // handles the response, adds the html
    function drawOutput(responseText) {
        var container = document.getElementById('text1');
        container.innerHTML = responseText;
    }
    // helper function for cross-browser request object
    function getRequest(url, success, error) {
        var req = false;
        try{
            // most browsers
            req = new XMLHttpRequest();
        } catch (e){
            // IE
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                // try an older version
                try{
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    return false;
                }
            }
        }
        if (!req) return false;
        if (typeof success != 'function') success = function () {};
        if (typeof error!= 'function') error = function () {};
        req.onreadystatechange = function(){
            if(req.readyState == 4) {
                return req.status === 200 ? 
                    success(req.responseText) : error(req.status);
            }
        }
        req.open("GET", url, true);
        req.send(null);
        return req;
    }


 </script>
</html>
Community
  • 1
  • 1
Fynn
  • 210
  • 1
  • 6
  • 28

2 Answers2

1

Because you should use .value instead of .innerHTML.

Reference: JavaScript get TextArea input via .value or .innerHTML?

Community
  • 1
  • 1
Alex Lau
  • 1,687
  • 10
  • 17
  • Thanks for the quick reply, that works! But now it outputs the whole source code (e.g. etc.) is it possible to just output the text? – Fynn Feb 24 '15 at 10:21
  • That would be another question: http://stackoverflow.com/questions/822452/strip-html-from-text-javascript – Alex Lau Feb 24 '15 at 10:22
  • 1
    Thanks, i got it to work with the help of that question. – Fynn Feb 24 '15 at 10:44
1

It is not setInnerHtml, textarea has a value attribute. Not really logical but well...

mad you a fiddle:

document.getElementById("bla").value = "test";
<textarea id="bla" readonly >Initial Value</textarea>
  • Thanks that works! But now it outputs the whole source code (e.g. etc.) is it possible to just output the text? – Fynn Feb 24 '15 at 10:27