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;       


        }


    }
Shaik
  • 930
  • 1
  • 20
  • 48
  • 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 – RiggsFolly Feb 07 '16 at 11:48
  • probably will not work if the code above is returned from an ajax call – Professor Abronsius Feb 07 '16 at 11:49
  • @A-2-A yes ur correct at that time it is not working – Shaik Feb 07 '16 at 11:50
  • @RamRaider why is it so, is there any work around for that – Shaik Feb 07 '16 at 11:51
  • 1
    @RiggsFolly how can php interpreter not be working if button exists? – charlietfl Feb 07 '16 at 11:51
  • @RiggsFolly i know that is there no possible way to do that is wat u meant to say – Shaik Feb 07 '16 at 11:52
  • Show the ajax code used – charlietfl Feb 07 '16 at 11:53
  • the callback function returns the contents as a string and inserts them into the document flow without them being parsed by the browser - they do not get interpreted in the manner you hope. I suspect that if the function `alerting` were to be defined as you would normally define a javascript function in the html flow then the click event would trigger the function correctly – Professor Abronsius Feb 07 '16 at 12:03
  • @RamRaider that works but i have a situation where i need in this way – Shaik Feb 07 '16 at 12:08
  • read the Dup question and answer, specially this answer that actually bothers to explain a little about the reason for your issue http://stackoverflow.com/a/2593099/2310830 – RiggsFolly Feb 07 '16 at 12:09
  • I do have a solution but the question is now closed alas. – Professor Abronsius Feb 07 '16 at 12:14

0 Answers0