我们如何在编写为ajax函数的php代码中显示JS警报

我们如何在编写为ajax函数的php代码中显示JS警报

问题描述:

I have an ajax function and while caling that function the php code will execute and check if any rows with the given data is present in the table. If there is no data I would like to display an alert. right now I can display a div with the error message but I could not get the out put such as

echo "<script> alert('error'); </script>";

我有一个ajax函数,在调用该函数时,php代码将执行并检查是否有任何具有给定数据的行 在表中。 如果没有数据我想显示警报。 现在我可以显示一个包含错误消息的div,但我无法获得输出,例如 p>

  echo“&lt; script&gt; alert('error');&lt;  / script&gt;“; 
  code>  pre> 
  div>

You can not stick JavaScript on the page using innerHTML since it will not be evaluated. What you would need to do is parse out the JavaScript code and shove it into eval(). OR use a framework that does it for you like jQuery.

It is better to develop a framework on the client that does not rely on this eval. A better messaging system would work.

{ "alert" : "Your alert message", "html" : " the mark up " }

Basic JavaScript idea:

//get the responseText
var result = xhr.responseText;
var json = JSON.parse(result); //This line is not cross browser for older browsers
if(json.alert){
  alert(json.alert);
}
if(json.html){
  document.getElementById("out").innerHTML = html;
}