How To Call Javascript In Ajax Response? IE: Close a form div upon success

后端 未结 3 877
甜味超标
甜味超标 2021-01-21 10:55

I have a form that when you submit it, it sends the data for validation to another php script via ajax. Validation errors are echo\'d back in a div in my form. A success messa

相关标签:
3条回答
  • 2021-01-21 11:03

    Your AJAX call should have a "success" callback. It looks like you can simply call displayDiv() in that callback.

    Also note that the var divstyle = new String(); line is unnecessary. Strings are immutable in JavaScript, so you are creating an empty string object, which remains unreferenced in the following line. Simply declare the variable when you assign it from document.getElementById():

    var divstyle = document.getElementById("myForm").style.display;
    
    0 讨论(0)
  • 2021-01-21 11:06

    This is a duplicate of How to make JS execute in HTML response received using Ajax? where I provided the chosen solution.

    var response = "html\<script type=\"text/javascript\">alert(\"foo\");<\/script>html";
     var reScript = /\<script.*?>(.*)<\/script>/mg;
     response = response.replace(reScript, function(m,m1) {
         eval(m1); //will run alert("foo");
         return "";
     });
    alert(response); // will alert "htmlhtml"
    
    0 讨论(0)
  • 2021-01-21 11:14
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
    
    //Since your making php do the validation, there would be two cases,
    //the first case is that the php script is not echoing any thing on success, and 
    //the other case is that its echoing the error massages which will be assignedxmhttp.responseText
    //so we need to check that xmlhttp.resposeText has been asigned a value.
    
       if(xmlhttp.resposeText){
           document.getElementById(displayContainers_id).innerHTML=xmlhttp.responseText;
       }
    }
    
    0 讨论(0)
提交回复
热议问题