Php 如果设置为$\u POST

Php 如果设置为$\u POST,php,isset,Php,Isset,我在一页上有一个表单,可以提交到另一页。在那里,它检查输入的邮件是否已填充。如果是这样,那么做一些事情,如果它没有被填满,那么做其他事情。我不明白为什么它总是说它已经设置好了,即使我发送了一个空表单。有什么遗漏或错误 step2.php: <form name="new user" method="post" action="step2_check.php"> <input type="text&q

我在一页上有一个表单,可以提交到另一页。在那里,它检查输入的
邮件是否已填充。如果是这样,那么做一些事情,如果它没有被填满,那么做其他事情。我不明白为什么它总是说它已经设置好了,即使我发送了一个空表单。有什么遗漏或错误

step2.php

<form name="new user" method="post" action="step2_check.php"> 
    <input type="text" name="mail"/> <br />
    <input type="password" name="password"/><br />
    <input type="submit"  value="continue"/>
</form>
if (isset($_POST["mail"])) {
    echo "Yes, mail is set";    
} else {    
    echo "N0, mail is not set";
}

使用
!空的
而不是设置的
。 isset为
$\u POST
返回true,因为
$\u POST
数组是超全局数组,并且始终存在(set)


或者最好使用
$\u服务器['REQUEST\u METHOD']=='POST'

如果您发送的表单为空,则仍将发送
$\u POST['mail']
,但值为空。要检查字段是否为空,需要检查

if(isset($_POST["mail"]) && trim($_POST["mail"]) != "") { .. }

大多数表单输入总是设置的,即使没有填充,所以您也必须检查是否有空

自从<代码>!empty()已经检查了这两个选项,您可以使用此选项:

if (!empty($_POST["mail"])) {
    echo "Yes, mail is set";    
} else {  
    echo "No, mail is not set";
}
,isset

如果var存在且值不是NULL,则返回TRUE,否则返回FALSE 否则


空空间被视为集合。您需要使用empty()来检查所有空选项。

也许您可以试试这个:

if (isset($_POST['mail']) && ($_POST['mail'] !=0)) { echo "Yes, mail is set"; } else { echo "No, mail is not set"; }


向输入文本表单添加以下属性:
required=“required”
。 如果表单未填写,则不允许用户提交表单

您的新代码将是:




if(isset($\u POST[“mail”])){
echo“是,邮件已设置”;
}

回答张贴的问题:isset和empty一起给出三个条件。Javascript也可以通过ajax命令使用这一点

$errMess="Didn't test";   // This message should not show
if(isset($_POST["foo"])){ // does it exist or not
    $foo = $_POST["foo"]; // save $foo from POST made by HTTP request
    if(empty($foo)){      // exist but it's null
        $errMess="Empty"; // #1 Nothing in $foo it's emtpy

    } else {              // exist and has data
        $errMess="None";  // #2 Something in $foo use it now
      }
} else {                  // couldn't find ?foo=dataHere
     $errMess="Missing";  // #3 There's no foo in request data
  }

echo "Was there a problem: ".$errMess."!";
您可以简单地使用:

if($_POST['username'] and $_POST['password']){
  $username = $_POST['username'];
  $password = $_POST['password'];
}
或者,使用

您可以尝试以下方法:

if (isset($_POST["mail"]) !== false) {
    echo "Yes, mail is set";    
}else{  
    echo "N0, mail is not set";
}



您可以试试

 <?php

     if (isset($_POST["mail"])) {
            echo "Yes, mail is set";    
        }else{  
            echo "N0, mail is not set";
        }
  ?>


让我们认为这是step2.php中的HTML表单

step2.php


其中$db是您的数据库连接。

检查表单是否已首先提交,然后是字段。您还应该清理该字段以防止黑客攻击

form name="new user" method="post" action="step2_check.php"> 
    <input type="text" name="mail"/> <br />
    <input type="password" name="password"/><br />
    <input type="submit"  id="SubmitForm" name= "SubmitForm" value="continue"/>
</form>
$-发布方法: 如果我们在表单标记中使用POST方法传递数据,我们可以使用$\u POST数组在服务器中查找数据。使用此数组名称,我们从服务器获取数据。($\u POST['name'])其他人看不到使用POST方法从表单发送的信息(所有名称/值都嵌入到HTTP请求体中)并且对发送的信息量没有限制。 示例代码:

<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
{if(isset($_POST['name']) && isset($_POST['roll']))
{echo"<h1>form submitted</h1>";echo"your name is". $_POST['name']."<br>";
echo "your roll number is". $_POST['roll']."<br>";}}
else{?>
<form action="" method="POST">
Name:<input type="text" name="name"><br><br>
Roll:<input type="text" name="roll"><br>
<br><input type="submit" value="submit" name="submit">
</form>
<?php}?>
</body>
</html>

名称:

滚动:



您应该提到同时使用
isset
!清空
,以防止出错。编辑:哎呀,很明显,我每天都在学习,然后我就试了,两种方法都很好。为什么$\u服务器['REQUEST\u METHOD']='POST'更好?它到底起什么作用?它是指特定的输入还是指表单的通用输入?
$\u服务器['REQUEST\u METHOD']
确保用户已提交表单
$\u POST
即使在这种情况下也可以为空。考虑此格式:<代码> <代码>,提交它将不发送任何动作,但请求类型将是“代码> POST < /代码>。也可以使用curl:
curl-xpost完成http://example.com/processor.php
。如果处理器包含类似于
echo$\u服务器['REQUEST\u METHOD'].'的代码。var_导出(空($_POST),1)
,当您在
$\u POST
中指定一个键并希望允许类似
0
的假值时,您将看到
POST true
isset()
可能是合适的。使用
发送电子邮件价值提交的专用工具!empty()
是一个不足以验证电子邮件地址的工具。我复制粘贴代码,但我认为它不起作用?你检查过了吗?为了记录在案,
if(isset($\u POST[“mail”])和trim($\u POST[“mail”]){
也会做同样的事情。这个比较表对像这样的小事非常有帮助……我认为在这种情况下最好避免使用
操作符(更容易阅读,出错的机会更小等等)如果(empty()){/*No*/}或者{/*Yes*/}我不明白为什么要避免
!empty())
。不过,我承认,我更喜欢在成功条件之前写失败条件。你需要解释你的答案。所以存在不仅是为了回答问题,也是为了教育人们。这个答案不会检查提交的电子邮件是否不是电子邮件。表单中的所有文本输入和文本区域都将提交到服务器即使它们的值是空字符串。你应该解释为什么你的代码回答OP的问题。以及为什么你不简单地推荐
!empty()
。根据,您是绝对正确的。一个简单的布尔值执行空函数的工作来检查空字符串。我知道这是一个旧答案,但第一个方法可能会引起未定义的索引通知。@ICE可以通过在变量前面加上
@
,即:
@$\u POST['username']
。感谢您注意到这一点。
@
通常会使代码看起来不专业。正确处理通知和警告。在检查
$Email
是否为空之前,请查看您是如何无条件声明的?这是在做不必要的工作。
!empty()
做两件事。它检查一个变量是否被声明,并检查它是否真实。因为变量肯定被声明了,所以你可以使用
if($Email){
,效果完全相同。我发现“用于检查所有空选项”对研究人员来说过于模糊或误导。仅依靠客户端验证是不够的。手动绕过客户端防御是微不足道的。为了提高安全性,需要在服务器端实现强验证。如本页其他地方所述
isset()
将无法按照OP的要求工作。我完全同意。将此答案写了很长时间。此答案表明事情的前端。理想情况下,验证应在前端和后端进行,错误从服务器端传递并在前端处理。研究底线
<?php
    if(isset($_POST['mail']) && $_POST['mail']!='') {
        echo "Yes, mail is set";
    }else{
        echo "N0, mail is not set";
    }
?>
<form name="new user" method="post" action="step2_check.php"> 
    <input type="text" name="mail"/> <br />
    <input type="password" name="password"/><br />
    <input type="submit"  value="continue"/>
</form>
if(isset($_POST['mail']) && !empty($_POST['mail']))
{
$mail = mysqli_real_escape_string($db, $_POST['mail']);
}
form name="new user" method="post" action="step2_check.php"> 
    <input type="text" name="mail"/> <br />
    <input type="password" name="password"/><br />
    <input type="submit"  id="SubmitForm" name= "SubmitForm" value="continue"/>
</form>

if (isset($_POST["SubmitForm"]))
   {
   $Email =  sanitize_text_field(stripslashes($_POST["SubmitForm"]));
   if(!empty($Email))
     echo "Yes, mail is set"; 
   else
     echo "N0, mail is not set";
   } 
}

<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
{if(isset($_POST['name']) && isset($_POST['roll']))
{echo"<h1>form submitted</h1>";echo"your name is". $_POST['name']."<br>";
echo "your roll number is". $_POST['roll']."<br>";}}
else{?>
<form action="" method="POST">
Name:<input type="text" name="name"><br><br>
Roll:<input type="text" name="roll"><br>
<br><input type="submit" value="submit" name="submit">
</form>
<?php}?>
</body>
</html>