表单提交没有刷新不起作用

时间:2016-11-05 22:16:53

标签: javascript php html

我正在尝试提交表单而不进行硬刷新,但我的页面很新鲜。我尝试将action=“javascript:void(0);"放在表单标记中,但这不起作用。我也不能使用jquery。

导致错误的工作流程:

  1. 提交一份应该将某些信息发送到php文件的表单。

  2. 页面被定向到另一个不存在的页面。

                       

        function toggle_visibility(id){
            var e = document.getElementById(id);
    
            if(e.style.visibility == 'hidden'){
                e.style.visibility = 'visible';
            }
            else{
                e.style.visibility = 'hidden';
            }
    
        }
        function createAjaxRequestObject() {
            var httpRequest;
            if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                httpRequest = new XMLHttpRequest();
                if (httpRequest.overrideMimeType) {
                    httpRequest.overrideMimeType('text/xml');
                }
            }
            else if (window.ActiveXObject) { // IE
                try {
                    httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                catch (e) {
                    try {
                        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e) {}
                }
            }
            if (!httpRequest) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
            }
            // Create the object
            return httpRequest;
        }
    
        function sendTicket() {
            var firstName = document.getElementById("firstName").value;
            var lastName = document.getElementById("lastName").value;
            var email = document.getElementById("email").value;
            var subject = document.getElementById("subject").value;
            var msg = document.getElementById("msg").value;
    
            var encFirst = encodeURIComponent(firstName);
            var encLast  = encodeURIComponent(lastName);
            var encEmail = encodeURIComponent(email);
            var encsubject = encodeURIComponent(subject);
            var encmsg = encodeURIComponent(msg);
    
            var info = "firstName="+encFirst+"&lastName="+encLast+"&email="+encEmail+"&subject="+encsubject+"&msg="+encmsg;
    
            var http3 = createAjaxRequestObject();
    
            if (http3.readyState == 4) {
                if (http3.status == 200){
                    var result = JSON.parse(http3.responseText);
                    console.log(result);
                }
            }
    
            http3.open("POST", "send_mail.php", true);
            http3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            http3.send(info);
    
        }
    
    </script>
    

    欢迎您!

            查看我的门票         提交门票         修改我的密码                                   

  3. 全名
  4.             
  5.                 电子邮件                              
  6.             
  7.                 学科                              
  8.             
  9.                 你的信息 *                              
  10.             
  11.                              
  12.                

1 个答案:

答案 0 :(得分:1)

您应该将type的{​​{1}}从submit更改为button,以便触发sendTicket(),否则表单会在到达{{1}之前被提交函数应该是:

sendTicket()

而不是:

<input type="button" value="Submit" onclick="sendTicket();" />

希望这有帮助。

&#13;
&#13;
<input type="submit" value="Submit" onclick="sendTicket();" />
&#13;
function sendTicket() {
  var firstName = document.getElementById("firstName").value;
  var lastName = document.getElementById("lastName").value;
  var email = document.getElementById("email").value;
  var subject = document.getElementById("subject").value;
  var msg = document.getElementById("msg").value;

  var encFirst = encodeURIComponent(firstName);
  var encLast  = encodeURIComponent(lastName);
  var encEmail = encodeURIComponent(email);
  var encsubject = encodeURIComponent(subject);
  var encmsg = encodeURIComponent(msg);

  var info = "firstName="+encFirst+"&lastName="+encLast+"&email="+encEmail+"&subject="+encsubject+"&msg="+encmsg;
  console.log(info);

}
&#13;
&#13;
&#13;