drv13270 2016-09-05 11:49
浏览 1034
已采纳

表单提交后重定向

I have a form set up and a javascript file (as shown below) that I want to validate the input and then redirect to a different website (index.html). The validation works perfectly, but I can't figure out how to get the form to redirect to the wanted page instead of just showing the post return. The form posts to a php file (which I am unable to access), and I would like the return of that file to then be displayed on index.html.

Form:

    <form name="form" action="register.php" onsubmit="return validateInput()" method="POST" >
      <fieldset>
        <legend>Personal Details</legend>
        <label>Name</label><br>
        <input type="text" name="name"><br><br>
        <label>Email</label><br>
        <input type="text" name="email"><br><br>
        <label>Date of Birth</label><br>
        <input type="text" name="dob"><br>
      </fieldset>
      <input type="submit" name="submit"> <input type="reset" name="reset">
    </form>

JavaScript File:

 function validateInput() {

    var check = document.forms["form"].elements;

    for (var i = 0; i < check.length; i++) {
        var test = check[i];

        if (test.type === "text" && test.value === "") {
            displayMsg("Please fill out all fields" +test.innerHTML);
            return false;
        }
        if (test.type === "date") {
            var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

            if (!test.value.match(pattern)) {
                displayMsg("Please enter a valid date of birth");
                return false;
            }
        }
        else if (test.name === "email") {
            var re = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

            if (!test.value.match(re)) {
                displayMsg("Please enter a valid email address");
                return false;
            }
        }
    }
    return true;
}

function displayMsg(msg) {
    var ele = document.getElementById("msg");
    ele.className = "show";
    ele.innerHTML = "<p>" + msg + "</p>";
}

window.onload = function() {
    var check = document.forms["form"].elements;
    for (var i = 0; i < check.length; i++) {
        check[i].addEventListener('click', function() {
            var ele = document.getElementById("msg");

            if(ele.className != "hide") {
                ele.className = "hide";
            }
        });
    }
}
  • 写回答

1条回答 默认 最新

  • dongliao9233 2016-09-05 12:01
    关注

    Instead of return true; you can set the location that you want using window.location.href and use return false; to prevent the default submission of the form:

    function validateInput() {
        ....
        window.location.href = 'index.html';
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?