Javascript does not work when including php file in ajax

I have a dropdown list and a button in which I inserted the onclick event, so that whenever someone clicks on this button, it calls a function that basically includes ajax, and when executed it will include a php file. In my php file, it takes the value from the selected dropdown list, after which it displays a warning, but it does not display any warnings.

Below is my code: -

<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","user.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users1" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

user.php

<?php
$q=$_GET["q"];
if($q ==1)
{
    echo '<script type="text/javascript">
    alert("Hello")
    </script>';
}
else {
    echo "No";
}
?>

Any help would be appreciated and thanks for your help in advance :)

+5
source share
9 answers

, , AJAX, , - , DOM, , :

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

. responseText javascript, . , .

, -, , AJAX. div txtHint, , . , onreadystatechange , txtHint div.

, . , "" "" , :

if (str=="1") alert("Hello");
else          alert("No");

, AJAX, , xmlhttp.responseText. , , user.php. Text, , txtHint.

+1

, script, , . json user.php, (/). , onreadystatechanged. "" / "" - :

<?php
$q=$_GET["q"];
if($q ==1)
{
    echo "Yes";
}
else {
    echo "No";
}
?>

:

if(xmlhttp.responseText === "yes"){
 alert("Hello");
}
else{
 alert("No");
}
0

HTML-, AJAX, .

document.getElementsByTagName('body')[0].appendChild(xmlhttp.responseText);

, , , , , , .

PHP- , AJAX, , , . PHP , script, .

EDIT: Javascript, , jQuery. jQuery , , , . , , appendChild() innerHTML .

: , , , , , xmlhttp.responseText document.createDocumentFragment():

var frag = document.createDocumentFragment();
frag.innerHTML = xmlhttp.responseText;
document.getElementsByTagName('body')[0].appendChild(frag);
0

showUser(this.options[this.selectedIndex].value) showUser(this.value)

Edit:

, $q=$_GET["q"]; $q=intval($_GET["q"]);

0

if(xmlhttp.responseText == "yes"){
 alert("Hello");
  }
else{
 alert("No");
 }

Swap === with ==

0

, innerHTML =... script DOM, () script. :

    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }

:

    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        var myScripts = document.getElementById("txtHint").getElementsByTagName("script");
        if(myScripts.length > 0) 
        {
            for(i=0; i < myScripts.length;i++)
            {
                eval(myScripts[i].innerHTML);
            }
        }
    }

, , , .

0
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="<b>Person info will be listed here.</b>";
  return;
}

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

     //////////// YOU CAN ADD THIS!///////////////////
    if (str=="1"){alert('hello');}
     ////////////////////////////////////////////////

    }
  }
xmlhttp.open("GET","user.php?q="+str,true);
xmlhttp.send();

}
</script>
</head>
<body>

<form>
<select name="users1" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>
0

Fiddler, , /, . , user.php? .

0

, innerHTML , . - DOM, jQuery, DOM API. , , - Javascript:

var jsTag = document.createElement('script');
var jsCode = "alert('this came from PHP or whatever you have on server side');";
jsCode = document.createTextNode(jsCode);
jsTag.appendChild(jsCode);
document.body.appendChild(jsTag);

or, with jQuery it will be something like:

$('body').append("<script>alert('this came from PHP or whatever you have on server side');</script>");

~ ~ amuses

0
source

All Articles