0

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>";

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.