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

表单提交后重定向

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 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示