如果有警报停止重定向save.php的Javascript表单

时间:2013-12-08 16:08:57

标签: javascript forms alert redirect

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

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)友好。 请帮忙

1 个答案:

答案 0 :(得分:0)

没有"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;
}