0

I am trying to echo a javascript along with the html button to a .html page via ajax but when i click on button console says there is no function to execute tho there is a function in the html body am not sure what is wrong my question seems to be silly but i googled a lot i dont find any proper solution

My PHP code

echo "<button type='button' onclick='alerting();'>Testing Script</button>";
echo '<script type="text/javascript">function alerting(){alert("dfdgfg");}</script>';   

Javascript

HttPRequest = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        HttPRequest = new XMLHttpRequest();
        if (HttPRequest.overrideMimeType) {
            HttPRequest.overrideMimeType('text/html');
        }
        } else if (window.ActiveXObject) { // IE
        try {
            HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
            try {
                HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    } 

    if (!HttPRequest) {
        return false;
    }


    var url = 'http://localhost/text.php';
    var pmeters = "tschoolid=" + encodeURI(localStorage.getItem("txtPassword1")) +
    "&tuserid=" + encodeURI(localStorage.getItem("Userid"));


    HttPRequest.open('POST',url,true);

    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    HttPRequest.setRequestHeader("Content-length", pmeters.length);
    HttPRequest.setRequestHeader("Connection", "close");
    HttPRequest.send(pmeters);


    HttPRequest.onreadystatechange = function()
    {

        if(HttPRequest.readyState == 3)  // Loading Request
        {


        }

        if(HttPRequest.readyState == 4) // Return Request
        {
            var retVal = HttPRequest.responseText;

            document.getElementById("Header_section").innerHTML = retVal;       


        }


    }
11
  • 2
    That code works for me! But only if it is held in a file with a .php extension. Otherwise the PHP interpreter will not get called and the echo will not get run, it will just get sent to the page as raw text Commented Feb 7, 2016 at 11:48
  • probably will not work if the code above is returned from an ajax call Commented Feb 7, 2016 at 11:49
  • @A-2-A yes ur correct at that time it is not working Commented Feb 7, 2016 at 11:50
  • @RamRaider why is it so, is there any work around for that Commented Feb 7, 2016 at 11:51
  • 1
    @RiggsFolly how can php interpreter not be working if button exists? Commented Feb 7, 2016 at 11:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.