how can we display JS alert in a php code written as ajax function
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 开发者_StackOverflowlike 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>";
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;
}
精彩评论