php 如果设置 $_POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13045279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:42:25  来源:igfitidea点击:

If isset $_POST

phpisset

提问by Nrc

I have a form on one page that submits to another page. There, it checks if the input mail is filled. If so then do something and if it is not filled, do something else. I don't understand why it always says that it is set, even if I send an empty form. What is missing or wrong?

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

step2.php:

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>

step2_check:

step2_check:

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

回答by oopbase

Most form inputs are always set, even if not filled up, so you must check for the emptiness too.

大多数表单输入总是被设置,即使没有填写,所以你也必须检查是否为空。

Since !empty()is already checks for both, you can use this:

由于!empty()已经对两者进行了检查,您可以使用它:

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

回答by Nemoden

Use !emptyinstead of isset. isset return true for $_POSTbecause $_POSTarray is superglobal and always exists (set).

使用!empty代替isset。isset 返回 true$_POST因为$_POST数组是超全局的并且始终存在(设置)。

Or better use $_SERVER['REQUEST_METHOD'] == 'POST'

或者更好的使用 $_SERVER['REQUEST_METHOD'] == 'POST'

回答by janenz00

From php.net, isset

来自 php.net, isset

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

如果 var 存在且具有非 NULL 值,则返回 TRUE,否则返回 FALSE。

empty space is considered as set. You need to use empty() for checking all null options.

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

回答by mboldt

If you send the form empty, $_POST['mail'] will still be sent, but the value is empty. To check if the field is empty you need to check

如果发送空表单,$_POST['mail'] 仍会发送,但值为空。要检查该字段是否为空,您需要检查

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

回答by CONvid19

You can simply use:

您可以简单地使用:

if($_POST['username'] and $_POST['password']){
  $username = $_POST['username'];
  $password = $_POST['password'];
}

Alternatively, use empty()

或者,使用empty()

if(!empty($_POST['username']) and !empty($_POST['password'])){
  $username = $_POST['username'];
  $password = $_POST['password'];
}

回答by Nadim Tareq

Use !empty()instead of isset(). Because isset()will always return true in your case.

使用!empty()代替isset()。因为isset()在你的情况下总是会返回 true 。

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

回答by Kushan Mehta

Add the following attribute to the input text form: required="required". If the form is not filled, it will not allow the user to submit the form.

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

Your new code will be:

您的新代码将是:

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

回答by Asep Nurjaman

<?php
    if(isset($_POST['mail']) && $_POST['mail']!='') {
        echo "Yes, mail is set";
    }else{
        echo "N0, mail is not set";
    }
?>

回答by AleksAnderson IT

Lets Think this is your HTML Form in step2.php

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

step2.php

步骤2.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>

I think you need it for your database, so you can assign your HTML Form Value to php Variable, now you can use Real Escape String and below must be your

我认为你的数据库需要它,所以你可以将你的 HTML 表单值分配给 php 变量,现在你可以使用真正的转义字符串,下面必须是你的

step2_check.php

step2_check.php

if(isset($_POST['mail']) && !empty($_POST['mail']))
{
$mail = mysqli_real_escape_string($db, $_POST['mail']);
}

Where $db is your Database Connection.

$db 是您的数据库连接。

回答by Debbie Kurth

Check to see if the FORM has been submitted first, then the field. You should also sanitize the field to prevent hackers.

检查是否已先提交表单,然后是字段。您还应该对现场进行消毒以防止黑客入侵。

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>

step2_check:

step2_check:


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