简体   繁体   English

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

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

I am trying to make a "form" of the type "text" which will check whether an email is valid or not. 我正在尝试制作“文本”类型的“表单”,该表单将检查电子邮件是否有效。 I first tried to check if the text field was empty by making an if statement, but it didnt work, and it instead ran the "else" option of the statement. 我首先尝试通过创建if语句来检查文本字段是否为空,但是它没有用,而是运行了该语句的“ else”选项。 How do I fix it so it actually checks if the text field is empty? 如何解决它,以便它实际上检查文本字段是否为空? Here is the code. 这是代码。

INDEX.php: 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 : 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";
}

?>

When doing: 进行时:

$email = $_POST['email'];

You are in fact setting $email so the !isset() will return false (meaning that it is NOT not isset) 实际上,您正在设置$email所以!isset()将返回false (这意味着不是isset)

instead you need to check if it is not empty 相反,您需要检查它是否不为空

if(!empty($email)){

}
else{

}

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

EXTRA Here is an example of how how you can check valid email addresses: 额外这是如何检查有效电子邮件地址的示例:

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

Also check out my PHP function that checks to see if the email is valid and can receive emails by connecting to the mail server and verifying it: 还要检查我的PHP函数,该函数检查电子邮件是否有效,并且可以通过连接到邮件服务器并进行验证来接收电子邮件:

https://github.com/hbattat/verifyEmail https://github.com/hbattat/verifyEmail

I did that. 我做到了 It did work when I removed the header code as well, but I want the echo to be displayed in index.php rather than in a new page (create.php). 当我也删除标题代码时,它确实起作用了,但是我希望将回显显示在index.php中,而不是在新页面(create.php)中显示。 How do i do that? 我怎么做? – tomSurge 3 hours ago – tomSurge 3小时前

When you submit to the create.php basically you load the create.php page with some post parameters. 当您提交到create.php基本上是使用一些post参数加载create.php页面。 So anything you display there will not be displayed in index.php because you are not in the same page. 因此,您显示的所有内容都不会显示在index.php因为您不在同一页面中。

Instead you could post to the same page index.php and do the create process there: 相反,您可以将其发布到同一页面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>

OR 要么

You could use AJAX and not load the page when posting the info to create.php and just display the response message: 您可以使用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 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() only checks if the variable is set. isset()仅检查是否设置了变量。 You need to use empty($email) instead. 您需要改用empty($email)

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

If I understand your question, you have to change the statement to if(!empty($email)) Also, if you want to check if the email is in the correct format, please use a regex to do so. 如果我理解您的问题,则必须将语句更改为if(!empty($email))此外,如果您要检查电子邮件的格式是否正确,请使用正则表达式进行操作。 isset() only checks for null isset()仅检查null

You can also test for submission of something (anything) in the posted email field with: 您还可以通过以下方式在已发布的电子邮件字段中测试是否提交了任何内容:

'' == $_POST['email']

As others have suggested, empty($_POST['email']) works too, just showing another option. 正如其他人所建议的那样, empty($_POST['email'])可以工作,只是显示另一个选项。

Displaying the Error Message 显示错误信息

If you want the error message to appear on index.php then you need to pass the message or some sort of flag from the create.php page (where the error is discovered) to the index.php page (where the error message is displayed). 如果您希望错误消息出现在index.php上,则需要将该消息或某种标记从create.php页面(发现错误的地方)传递到index.php页面(显示错误消息的地方) )。

To demonstrate a bare-bones example (one of many ways to accomplish this), first I added a PHP variable to the form. 为了演示一个简单的示例(完成此操作的多种方法之一),首先,我向表单添加了一个PHP变量。 Notice the line containing echo $_GET['error']; 注意包含echo $_GET['error']; immediately after the email input element: 在电子邮件输入元素之后:

index.php (form): 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>

...and, instead of echo 'ing the message in the create.php script, send the message along to the index.php script in the URL query: ...,而不是在create.php脚本中echo显消息,而是将消息发送到URL查询中的index.php脚本:

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

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

Additionally, I recommend you use this (PHP server-side validation) as a backup to a Javascript client-side validation solution. 另外,我建议您使用此(PHP服务器端验证)作为Javascript客户端验证解决方案的备份。 Nicer for your website visitors and uses fewer server resources. 为您的网站访问者提供更好的服务,并减少服务器资源。

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

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

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