繁体   English   中英

PHP代码检查字段是否为空不起作用

[英]PHP code checking if field is empty is not working

我正在尝试制作“文本”类型的“表单”,该表单将检查电子邮件是否有效。 我首先尝试通过创建if语句来检查文本字段是否为空,但是它没有用,而是运行了该语句的“ else”选项。 如何解决它,以便它实际上检查文本字段是否为空? 这是代码。

index.php文件:

<?php
include 'connection.php';
echo  "<p> Example Text </p>";



?>

<h1> Submit email address </h1>

<form action = "create.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />

<input type = "submit" name = "submit" />

</form>

Create.php:

<?php
include 'connection.php';
$email = $_POST['email'];

if(!isset($email)){
echo "please fill out the form";
header ('Location: index.php');
}
else{
echo "Successive login";
}

?>

进行时:

$email = $_POST['email'];

实际上,您正在设置$email所以!isset()将返回false (这意味着不是isset)

相反,您需要检查它是否不为空

if(!empty($email)){

}
else{

}

================

额外这是如何检查有效电子邮件地址的示例:

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  echo("$email is a valid email address");
} else {
  echo("$email is not a valid email address");
}

还要检查我的PHP函数,该函数检查电子邮件是否有效,并且可以通过连接到邮件服务器并进行验证来接收电子邮件:

https://github.com/hbattat/verifyEmail

我做到了 当我也删除标题代码时,它确实起作用了,但是我希望将回显显示在index.php中,而不是在新页面(create.php)中显示。 我怎么做? – tomSurge 3小时前

当您提交到create.php基本上是使用一些post参数加载create.php页面。 因此,您显示的所有内容都不会显示在index.php因为您不在同一页面中。

相反,您可以将其发布到同一页面index.php并在那里进行创建过程:

<?php
include 'connection.php';
if($_SERVER['REQUEST_METHOD'] == 'post'){ //check if the page was requested via post method (that means the form was submitted)
  if(!isset($email)){
     echo "please fill out the form";
  }
  else{
    echo "Successive login";
  }
}
echo  "<p> Example Text </p>";
?>

<h1> Submit email address </h1>

<form action = "index.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />

<input type = "submit" name = "submit" />

</form>

要么

您可以使用AJAX并在将信息发布到create.php时不加载页面,而仅显示响应消息:

<?php
include 'connection.php';
echo  "<p> Example Text </p>";
?>

<!-- include jQuery in the header of your html -->
<script src="LINK TO jQUERY .js file"></script>

<script type="text/javascript">
$(document).ready(function(){
  //trigger this when the form is submitted
  $('form').on('submit', function(e){
    //prevent default submission and reload of the page
    e.preventDefault();

    //post the date to the create.php file using ajax
    //get the form data
    var formData = $(this).serialize();
    $.post('create.php', fomrData, function(response){
       var result = parseJSON(response);
         $('#msg').text(result.msg);
       }
    });
  });
});
</script>

<h1> Submit email address </h1>

<form action = "create.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />

<input type = "submit" name = "submit" />

</form>

create.php

<?php
include 'connection.php';
$email = $_POST['email'];

$result = array();
if(!isset($email)){
  $result['status'] = 'fail';
  $result['msg'] = "please fill out the form";
}
else{
  $result['status'] = 'success';
  $result['msg'] = "Successive login";
}

//echo the json
echo json_encode($result);
?>

isset()仅检查是否设置了变量。 您需要改用empty($email)

isset替换为empty ,它应该可以根据需要工作。

如果我理解您的问题,则必须将语句更改为if(!empty($email))此外,如果您要检查电子邮件的格式是否正确,请使用正则表达式进行操作。 isset()仅检查null

您还可以通过以下方式在已发布的电子邮件字段中测试是否提交了任何内容:

'' == $_POST['email']

正如其他人所建议的那样, empty($_POST['email'])可以工作,只是显示另一个选项。

显示错误信息

如果您希望错误消息出现在index.php上,则需要将该消息或某种标记从create.php页面(发现错误的地方)传递到index.php页面(显示错误消息的地方) )。

为了演示一个简单的示例(完成此操作的多种方法之一),首先,我向表单添加了一个PHP变量。 注意包含echo $_GET['error']; 在电子邮件输入元素之后:

index.php(窗体):

<form action="create.php" method="post">
<input type="text" name="email" value="" />
<?php if (isset($_GET['error'])) { echo $_GET['error']; } ?>
<br />

<input type="submit" name="submit" />

</form>

...,而不是在create.php脚本中echo显消息,而是将消息发送到URL查询中的index.php脚本:

create.php(电子邮件检查):

if ( '' == $_POST['email'] ) {
  header('Location: index.php?error=please+fill+out+the+form');
}

另外,我建议您使用此(PHP服务器端验证)作为Javascript客户端验证解决方案的备份。 为您的网站访问者提供更好的服务,并减少服务器资源。

另一种方法是在输入中添加一个required="" ,这是一个示例https://jsfiddle.net/57s38s2e/

暂无
暂无

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

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