1

I have a textarea that I want to fill using ajax if javascript is enabled in the user's browser, but use php if js is disabled. The problem is, both the ajax and the php will execute, creating a double of every input entered. I tried a noscript tag, but then nothing prints out. I would greatly appreciate any help.

Here's my html:

<form action="savemessage.php" method="post" id="mainChat" name="mainChat"
    onsubmit="return loadChat();">
<textarea class="historyClass" id="historyFieldjs" name="historyFieldjs" rows="40" cols="80">
<noscript>
<textarea class="historyClass" id="historyField" name="historyField" rows="40" cols="80">
<?php
echo "stuff";
?>
</textarea></noscript>

And javascript:

function loadChat()
{
    var xmlhttp = new XMLHttpRequest;
    xmlhttp.open("GET", "getmessages.php", true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var results = JSON.parse(xmlhttp.responseText);
            var i;
            for(i=0; i<results.length; i++)
            {
                document.getElementById("historyFieldjs").value=
                    document.getElementById("historyFieldjs").value+result[i]+"\n";
            }
        }
    }
    return false;
}
4
  • 1
    Have PHP fill the field, and if JavaScript is enabled, clear the field then load the data. Commented Jun 28, 2013 at 17:55
  • @RocketHazmat post answers as answers, not comments Commented Jun 28, 2013 at 18:24
  • You shouldn't waste your time making something support JavaScript disabled. JavaScript is a web standard and is enabled in all browsers by default. The only reason someone would disable it is to attempt to bridge some site security or functionality. I say to hell with them. Commented Jun 28, 2013 at 18:43
  • the reason your noscript fails is because you call document.getElementById("historyField"), which is never there to js. or wait, nvrmind, you set it twice... Commented Jun 28, 2013 at 19:22

1 Answer 1

2

One solution is to always have PHP fill in the field. Then, when your page loads, if JavaScript is enabled, it can clear the field and then load the data into it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.