2016-06-01 232 views
1

我是一个PHP的总新手,这就是为什么我总是使用我的联系表格阅读代码。事情是我不知道如何防止空的输入被发送到我的电子邮件。我有一个输入表单,我想验证它。我真的搜索了一个解决方案,但不幸的是它不工作。我希望有一个人可以帮助我。我把我在这里的代码:如何防止我的表单中的空输入(HTML和PHP)

<?php 
if (isset($_POST["submit"])) { 
    $email = $_POST['email']; 
    $from = 'Felipe Felix'; 
    $to = '[email protected]'; 
    $subject = 'My email listing'; 

    $body = "E-mail: $email"; 

    if (mail ($to, $subject, $body, $from)) { 
    header("Location: index.html/success.html"); 
    } else { 
    header("Location: index/error.html"); 
    } 
} 
?> 

而我的HTML:

<form id="emailListing" class="form-horizontal" action="index.php" method="POST" enctype="multipart/form-data" name="sentMessage" data-fv-framework="bootstrap" data-fv-icon-valid="glyphicon glyphicon-ok" data-fv-icon-invalid="glyphicon glyphicon-remove" data-fv-icon-validating="glyphicon glyphicon-refresh"> 
    <div class="form-group"> 
    <div class="col-xs-8 col-lg-9"> 
     <input id="email" class="form-control" name="email" type="email" placeholder="メールアドレス" data-fv-emailaddress-message="This is not a valid email" /> 
    </div> 
    <input id="submit" name="submit" type="submit" value="Submit" class="btn btn-default col-xs-4 col-lg-3"> 
    </div> 
</form> 

我也是用这个(这是我的另一个问题所看到的,但它不工作我猜):

<script> 
    $(document).ready(function() { 
    $('#emailListing').formValidation(); 
    }); 
</script> 

回答

0

你可以仔细检查空场都客户端和服务器端:

submis之前,使用客户端的jQuery锡永:从提交后PHP

$(document).ready(function() { 
    $("#emailListing").on('submit', function(e) { 
     e.preventDefault(); //to prevent submit before check 

     if ($("#email").val()!=''){ 
      $("#emailListing").submit(); 
     } 
     else 
     { 
     alert('Please fill email field'); 
     } 
    }); 
}); 

服务器端:

if (!empty($_POST[email])){ 
// Do something 
} 
+0

你好,阿里。当我试图在没有输入的情况下发送消息时,我看到了错误信息,没关系!但是现在如果我输入一个有效的电子邮件,没有任何反应也许我错过了一些在PHP中的东西。我将使用您的建议更新我的文章。 –

+0

我建议把你的问题回到以前的版本,并提出一个新的问题,这样的答案对他人有用,然后张贴新问题的链接,所以我可以在那里帮助。 –

+0

好的,就这样做了。我不认为用相同的主题来开一个新的问题是一个不错的想法。 –

2

对于您必须required='required'或仅required属性,在输入 - (HTML5功能),在HTML页面之间不得空如果你想在提交形式和使用它像

<input id="email" class="form-control" name="email" type="email" placeholder="メールアドレス" data-fv-emailaddress-message="This is not a valid email" required /> 

在前端的jQuery验证你可以有很多插件,但你需要包括寿使用<script>标签,在后端进行验证HTML页面本身的代码,你只需要

<?php 
if (isset($_POST["submit"])) { 
    $email = $_POST['email']; 
    if(empty($email)) 
    { 
     header("Location: index/error.html"); 
    } 
    $from = 'Felipe Felix'; 
    $to = '[email protected]'; 
    $subject = 'My email listing'; 

    $body = "E-mail: $email"; 

    if (mail ($to, $subject, $body, $from)) { 
     header("Location: index.html/success.html"); 
    } else { 
     header("Location: index/error.html"); 
    } 
} 
?> 
+0

Hello Veshraj,感谢您的回复!不幸的是,它没有奏效。我用其他发给我答案的人的回复编辑了我的问题。随着他的回答,我可以开始看到空输入的错误信息,但现在我无法发送电子邮件。也许你知道错误在哪里? –

1

试试这个..

<?php 
     if (isset($_POST["submit"])) { 
     $email = $_POST['email']; 
     $from = 'Felipe Felix'; 
     $to = '[email protected]'; 
     $subject = 'Contact'; 
$body = "E-mail: $email"; 

if ($_POST['email']){ 
    if (mail ($to, $subject, $body, $from)) { 
    header("Location: index.html"); 
} 
else { 
    header("Location: index.html"); 
    } 
    } 
else{  
header("Location: index/error.html"); 
} 
} 
?> 
+0

你好@abhishek!感谢您在我的查询上花费一些时间。使用你的建议,我能够正确地得到错误信息,尽管我仍然能够发送空信息。我找到了另一个解决方案,并会尽快发布。 –

0

的事情是,尝试一些建议其他用户后给我,我最终会得到一个表单,如果你输入它的话会发送邮件地址,但即使你让它为空(错误页面发送,无论如何也会发送邮件)。所以,我尝试了另一种方式,它的工作!我的表单现在阻止用户发送空输入,并在用户发送有效的电子邮件地址时向他们提供反馈。我不知道它在语法上是否100%正确,但我没有收到任何错误。谢谢大家的支持!我把我在这里的代码:

PHP

<?php 
if (isset($_POST["submit"])) { 
    $email = $_POST['email']; 
    $from = 'Me'; 
    $to = '[email protected]'; 
    $subject = 'My Subject'; 
    $body = "E-mail Address: $email"; 

    if ($_POST['email']){ 
    if (mail ($to, $subject, $body, $from)) { 
     header("Location: http://website.com/success.html"); 
    } 
    else{ 
     header("Location: http://website.com/error.html"); 
    } 
    } 
    else{ 
    header("Location: http://website.com/error.html"); 
    } 
} 
?> 

HTML(我使用的引导,这就是为什么所有这些类)

<form id="emailListing" class="form-horizontal" action="index.php" method="POST" enctype="multipart/form-data" name="sentMessage" onsubmit="return validateForm()"> 
    <div class="form-group"> 
    <div class="col-xs-8 col-lg-9"> 
     <input id="email" class="form-control" name="email" type="email" placeholder="E-mail Address" /> 
    </div> 
    <input id="submit" name="submit" type="submit" value="Subscribe" class="btn btn-default col-xs-4 col-lg-3"> 
    </div> 
</form> 

JS

<script> 
    function validateForm() { 
    var x = document.forms["sentMessage"]["email"].value; 
    if (x == null || x == "") { 
     alert("メールアドレスを入力してください"); 
     return false; 
    } 
    } 
</script> 
相关问题