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 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题