简体   繁体   English

使用onClick提交PHP表单

[英]Submitting a PHP form with onClick

So I currently have a download link and an input field for an email address on my website. 所以我目前在我的网站上有一个下载链接和一个电子邮件地址的输入字段。

In order to download the file you first need to put in your email. 要下载文件,首先需要输入您的电子邮件。

I use a form to do this, with the email field being an input field and the download button being a submit button. 我使用表单来执行此操作,电子邮件字段是输入字段,下载按钮是提交按钮。

I like HTML5's form validation (the required fields, field types etc, it all looks very nice). 我喜欢HTML5的表单验证(必填字段,字段类型等,它看起来都非常好)。

The problem is that if I use onClick in my submit button then none of the nice form validation works. 问题是如果我在提交按钮中使用onClick,那么没有一个好的表单验证工作。

<form>
     <input type="email" id="email" placeholder="Please enter email" required>

     <input type="submit" class="btn" onclick="downloadWin()" value="Windows">
     <input type="submit" class="btn" onclick="downloadOsx()" value="Osx">  
</form>


<script>
    function downloadWin(){
        event.preventDefault();
        var email = $("#email").val();
        if(email != ''){
            if(validateEmail(email)){
                location.href='http://s/index.php?page=downloadWin&email='+email;
            }
        }
    }

    function downloadOsx(){
        event.preventDefault();
        var email = $("#email").val();
        if(email != ''){
            if(validateEmail(email)){
                location.href='http://s/index.php?page=downloadOsx&email='+email;
            }
        }
    }

</script>

This might not be the cleanest way to do it, so please if you think you know a better way tell me :) 这可能不是最干净的方法,所以如果你认为你知道更好的方式请告诉我:)

try this code 试试这段代码

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
function downloadWin() {
    var email = $("#email").val();
    if (email != '') {
        if (validateEmail(email)) {
            location.href = 'http://s/index.php?page=downloadWin&email=' + email;
        }
    }
    return false;
}

function downloadOsx() {
    var email = $("#email").val();
    if (email != '') {
        if (validateEmail(email)) {
            location.href = 'http://s/index.php?page=downloadOsx&email=' + email;
        }
    }
    return false;
}

Try this: 试试这个:

<form onsubmit="download(this.email.value,this.system.value)" id="form">
    <input type="email" id="email" name="email" placeholder="Please enter email" required>
    <input type="radio" name="system" value="Win" required >Windows
    <input type="radio" name="system" value="Osx" >Osx
    <input type="submit" class="btn"  value="Download"> 
</form>

<script type="text/javascript">
document.getElementById("form").addEventListener("submit", function(event){
    event.preventDefault();
});

function download(email_value,sys_value){
    location.href='http://s/index.php?page=download'+sys_value+'&email='+email_value;
}
</script>

Result: 结果:

在此输入图像描述

Below is the working code snippet (without using HTML5 validation). 以下是工作代码段(不使用HTML5验证)。 You can run and test it. 您可以运行并测试它。 I have used the jquery with jquery.validate plugin. 我已经将jqueryjquery.validate插件一起使用了。 You can uncomment the commented code to redirect user to the target url. 您可以取消注释注释代码以将用户重定向到目标网址。 Let us know if this what you are looking for or not. 如果您正在寻找或不寻找,请告诉我们。 Feel free to comment if there is anything that you feel confusing. 如果有任何您感到困惑的话,请随意发表评论。

 $(document).ready(function(){ $(".btn-download").on("click", function(e) { e.preventDefault(); e.stopImmediatePropagation(); if ($("#validateForm").valid()) { var name = $(this).val(); var email = $("#email").val(); if (name === "Windows") { //location.href = 'http://s/index.php?page=downloadWin&email=' + email; console.log('http://s/index.php?page=downloadWin&email=' + email); } if (name === "Osx") { console.log('http://s/index.php?page=downloadOsx&email=' + email); //location.href = 'http://s/index.php?page=downloadOsx&email=' + email; } } }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script> <form method="post" action="" id="validateForm" novalidate> <input type="email" id="email" placeholder="Please enter email" required> <input type="submit" name="btnSubmit" class="btn btn-download" value="Windows"> <input type="submit" name="btnSubmit" class="btn btn-download" value="Osx"> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM