duanrang2627 2015-03-12 22:51
浏览 31
已采纳

检查字段是否为空的PHP代码无效

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. How do I fix it so it actually checks if the text field is empty? Here is the code.

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";
}

?>
  • 写回答

6条回答 默认 最新

  • dtp19819 2015-03-12 22:56
    关注

    When doing:

    $email = $_POST['email'];
    

    You are in fact setting $email so the !isset() will return false (meaning that it is NOT not 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:

    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). How do i do that? – tomSurge 3 hours ago

    When you submit to the create.php basically you load the create.php page with some post parameters. So anything you display there will not be displayed in index.php because you are not in the same page.

    Instead you could post to the same page index.php and do the create process there:

    <?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:

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'