1

This is the html div:

<div class="alert alert-success alert-dismissable feedstatus hidden">
                          <button type="button" class="close" data-dismiss="alert">&times;</button>
                          <span class="glyphicon glyphicon-ok-sign" style="color:#0c0; font-size:40px;"> </span></a><br/>Message recieved.<br/>Thanks <strong class="feedbacker"></strong> 
                        </div>
                        <form role="form" onsubmit="regFeedback()">
                            <div class="form-group">
                                <input class="form-control bordering" id="feedbacker" type="text" name="name" placeholder="Name" style="border:none; border:0px solid #fff;" required/><br>
                                <input class="form-control bordering" id="feedbackemail" type="email" name="email" placeholder="tu@email.com" style="border:none; border:0px solid #fff;"  required/><br>
                                <textarea class="form-control bordering" id="feedback" name="feedback" placeholder="Write me something before go" style="border:none; border:0px solid #fff;"  required/></textarea><br>
                                <button type="submit" class="btn btn-block">Send</button>
                            </div>
                        </form>
                    </div>

In the other hand, the javascript:

function regFeedback() {
var name=document.getElementById('feedbacker').value;
var mail=document.getElementById('feedbackemail').value;
var feed=document.getElementById('feedback').value;
var xmlhttp;
if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
 } else {
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
   xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
        $(document).ready(function() {
           $("form").remove();
           $(".feedstatus").removeClass("hidden");
           $(".feedbacker").text(name);
           alert("Mensaje recibido. Gracias");
       });
   }
   };
    xmlhttp.open("GET","https://page.php?name="+name+"&email="+mail+"&feedback="+feed,true);
   xmlhttp.send();
   }

The data arrive to the data base, but the callback event where verifies: "if (this.readyState==4 && this.status==200) {" looks like don't work. I'm new in this, but i made it work well. But from a moment editing to another, something is wrong :/

Abax
  • 54
  • 6

3 Answers3

0

Not tested but I think instead of

 if (this.readyState==4 && this.status==200) 

will be

if (xmlhttp.readyState==4 && xmlhttp.status==200) 

this is a keyword in javascript & it refers to an object; that is, the subject in context, or the subject of the executing code

Also it is surprising so see the document.ready function is inside ajax success function

brk
  • 48,835
  • 10
  • 56
  • 78
0

Use this:

<div class="alert alert-success alert-dismissable feedstatus hidden">
      <button type="button" class="close" data-dismiss="alert">&times;</button>
      <span class="glyphicon glyphicon-ok-sign" style="color:#0c0; font-size:40px;"> </span></a><br/>Message recieved.<br/>Thanks <strong class="feedbacker"></strong> 
    </div>
    <form role="form">
        <div class="form-group">
            <input class="form-control bordering" id="feedbacker" type="text" name="name" placeholder="Name" style="border:none; border:0px solid #fff;" required/><br>
            <input class="form-control bordering" id="feedbackemail" type="email" name="email" placeholder="tu@email.com" style="border:none; border:0px solid #fff;"  required/><br>
            <textarea class="form-control bordering" id="feedback" name="feedback" placeholder="Write me something before go" style="border:none; border:0px solid #fff;"  required/></textarea><br>
            <button type="button" onclick="regFeedback()" class="btn btn-block">Send</button>
        </div>
    </form>
</div>
Pang
  • 9,564
  • 146
  • 81
  • 122
  • **You are using button as submit, use this as a button and call javascript at button onclick Replace your Html code with this:** – sangram singh May 09 '17 at 07:40
0

You have to use your xmlhttp object while checking for state change instead of this.

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {       
       $("form").remove();
       $(".feedstatus").removeClass("hidden");
       $(".feedbacker").text(name);
       alert("Mensaje recibido. Gracias");       
}
};

Also you are not require to perform action on $(document).ready() function, you can perform actions directly in success block.

Nishesh Pratap Singh
  • 2,131
  • 2
  • 13
  • 20