2017-04-06 77 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 
}