简体   繁体   中英

Prevent php from executing if javascript is enabled

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM