繁体   English   中英

JavaScript表单(如果有警报)停止重定向save.php

[英]Javascript form if have alert stop redirecting save.php

我有一封电子邮件表格。 我要阻止空白电子邮件发送和无效的电子邮件发送。 所以我做了这个;

index.php(带有表单)

    <script type="text/JavaScript">

function validateURL(url) {
    var reurl = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
    return reurl.test(url);
}

function validateForm()
{
    // Validate Email
    var email = $("#fremail").val();
    if ((/(.+)@(.+){2,}\.(.+){2,}/.test(email)) || email=="" || email==null) { } else {

        alert("Please enter a valid email");
        exit;

    }
      return false;
      }
</script>
    <form  id="form1" action="save.php" method="post" onsubmit="validateForm();" >
        <div class="data">
        <label><center>Be the first to know when we launch.</center></label>
            <div class="input-form1 center">
                <input  type="text" name="email" id="fremail" placeholder="Your e-mail goes here" class="input-text" required="required">
             <input type="submit" class="send-btn" value="Get on the list" ></center>

            </div>
        </div>

    </form>

Save.php(提交表单时)

<?php 
ob_start();
$dosya = "email.txt";             
$email = $_POST["email"];      
if($email==""){ 
echo "<h1>Email box cant be empty please <a href='index.php'>go back and fill it</h1></a>'";
die();  
}else{ 
       $ac1 = @fopen($dosya, "r"); 
        $icerik = @fread($ac1, filesize($dosya)); 
        @fclose($ac1); 

        $maildizi = explode(',', $icerik); 

        if(in_array($email,$maildizi)){ 
        echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL=index2.php\">";
        die(); 
        }else{ 

            $ac = fopen($dosya, "a") or die("$dosya dosyası açılamadı.");     

            $veri .= $email.","; 
            $yaz = fwrite($ac, $veri); 

                 if($yaz){ 
                echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL=index2.php\">";

                }else{ 
                echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL=index2.php\">";
                } 
        fclose($ac); 

        } 
        unset($maildizi); 
} 
                 ob_flush();
?>

在save.php里面,我有index2.php(表单提交成功页面)

它必须对移动设备(iPhone,Android)友好。 请帮忙

没有"exit;"

您需要return false并向onsubmit添加return语句

... method="post" onsubmit="return validateForm();" >

function validateForm() {
    // Validate Email
    var email = $("#fremail").val();
    var state = true;
    if (!(/(.+)@(.+){2,}\.(.+){2,}/.test(email)) || !email.length) {    
        alert("Please enter a valid email");
        state = false;    
    }
    return state;
}

暂无
暂无

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

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