2017-04-06 36 views
0

我有一個簡單的PHP電子郵件表格,但我收到垃圾郵件。我添加了一個檢查輸入,用戶必須填寫輸入。如果等於12的輸入值,然後提交表單,否則不要用彈出錯誤提交表單(請做數學題。)檢查輸入值是否等於整數然後不提交表格

<form action="" method="post"> 
<input type="text" name="name" placeholder="name" required> 
<input type="text" name="email" placeholder="email" required> 
<input type="text" name="phone" placeholder="phone" required> 

<div class="check"> 
    <label>6 x 2 =</label> 
    <input type="text" name="not_robot" required="required"> 
</div> 

<input type="submit" value="submit"> 
</form> 

我使用下面的方法PHP:

if(isset($_POST['not_robot']) !== 12 && isset($_POST['not_robot']) !== ''){ 
    echo '<script type="text/javascript">alert("Please do the math, if you are human.");</script>'; 
}else{ 
    //email script here 
} 

當我提交表單,錯誤彈出窗口出現說「請做數學,如果你是人類」,但在我關閉彈出窗口後,它也發送電子郵件。 任何幫助appreaciated謝謝

P.S:如果檢查方法是可能的使用JavaScript或jQuery這將是一個很大的幫助。

回答

0

嘗試檢查提交表單時。下面的代碼去或鏈路

JSFiddle

HTML代碼 -

<form action="" method="post"> 
    <input type="text" name="name" placeholder="name" required> 
    <input type="text" name="email" placeholder="email" required> 
    <input type="text" name="phone" placeholder="phone" required> 

    <div class="check"> 
    <label>6 x 2 =</label> 
    <input type="text" name="not_robot" required="required"> 
    </div> 

    <input type="submit" value="submit"> 
</form> 

JAVASCRIPT代碼 -

$('form').submit(function() { 
     if (parseInt($('input[name="not_robot"]').val()) == 12) { 
     return true; 
     } 
     else{ 
     alert('You not enterd the correct value'); 
     return false; 
     } 
    }); 
+0

這不是被問到的。 12只是一個例子 - 在測試布爾值 – mplungjan

+0

@Shubham後返回布爾值也沒有意義。謝謝,你的代碼正在工作,但如果值不等於12,我可以添加一條錯誤消息嗎? –

+0

@SajjadAhmad我已經更新了我的代碼,您可以在'else'部分打印任何警報消息。如果我的回答可以幫助你,請註冊或標記爲正確。 –

1

您需要在客戶端上測試:

平原JS

window.onload=function() { 
    document.querySelector("form").onsubmit=function(e) { 
    var val = this.elements["not_robot"].value; 
    return !isNaN(val) && parseInt(Number(val)) == val && !isNaN(parseInt(val, 10); 
    } 
} 

的jQuery:

$(function() { 
    $("form").on("submit",function(e) { 
    var val = $("[name='not_robot'"].val(); 
    if (isNaN(val) || !parseInt(Number(val)) == val || isNaN(parseInt(val, 10)) e.preventDefault(); 
    } 
} 
0

不要試圖阻止發送垃圾郵件這種方式。我的建議是應用csrf保護和谷歌驗證碼。您可以使用this library進行csrf保護。併爲谷歌驗證碼使用this

+1

這取決於目標受衆。計算器足以阻止收割機,但不是專門的垃圾郵件發送者 – mplungjan

-1

HTML:

<form action="" method="post" name="form1"> 
<input type="text" name="name" placeholder="name" required> 
<input type="text" name="email" placeholder="email" required> 
<input type="text" name="phone" placeholder="phone" required> 

<div class="check"> 
    <label>6 x 2 =</label> 
    <input type="text" name="not_robot" id="not_robot" required="required" /> 
</div> 

<input type="button" value="submit" onClick="checkForm()"> 
</form> 

javascipt的:

function checkForm() 
{ 

    if(document.getElementById('not_robot').value == "12" && document.getElementById('not_robot').value != "") 
    { 
    alert('ok'); 
    document.forms['form1'].submit(); 
    }else{ 
     alert('not ok'); 
     } 

} 
+0

他想在PHP中沒有JS –

+0

@niandrei代碼無法正常工作。 –

+0

我使用JSFiddle測試了代碼,它的工作原理如下:https://jsfiddle.net/2t23x0ud/如果數學答案錯誤,表單不會提交。 – niandrei

0

如果你想通過PHP..Below驗證的方式是

if(is_numeric($_POST['not_robot']) && !empty($_POST['not_robot'])){ 
    echo '<script type="text/javascript">alert("Please do the math, if you are human.");</script>'; 
}else{ 
    //email script here 
} 
+0

這不會停止提交 – mplungjan

0

方式:

  • 使用PHP exit()功能退出當前腳本。execution.we能使用此功能(或不使用)消息。

    語法:exit(message)ORexit()

  • 我們也可以使用return;

    這裏,控制將返回到調用該文件的運行該腳本。

試試這個:

if(isset(($_POST['not_robot']) != '12') && (isset($_POST['not_robot']) !== '')){ 
    /* alert here */ 
    exit(); 
}else{ 
    //email script here 
}