php 如果 $_POST 为空函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10949066/
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-24 23:25:35  来源:igfitidea点击:

If $_POST is empty function

phpformsfunction

提问by atif

I am creating a form where I am using $_POST php function.

我正在创建一个使用 $_POST php 函数的表单。

Here is how my form looks like.

这是我的表格的样子。

form.php

表单.php

<h1>Songs Movies Form</h1>
<hr />

<form action="songs-movies.php" method="post">

Song Name:
Enter Song Name: <input type="text" name="SongName" size="25" />

Enter Movie Name: <input type="text" name="MovieName" size="25" />

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

</form>

<br />
<a href="/forms/">Back to Forms</a>
<hr />

Now I want to display result of SongNameand MovieName, so I used echo $_POST["SongName"];and echo $_POST["MovieName"];which generates the result perfectly.

现在我想显示SongNameMovieName 的结果,所以我使用了echo $_POST["SongName"]; 回显 $_POST["MovieName"]; 这完美地产生了结果。

Now I want to put these values SongNameand MovieNamebetween the text/line/para in the result/output page and if the value of SongNameor MovieNameis empty then the result/output should not display that specific text/line/para where I have put those functions.

现在我想将这些值SongNameMovieName放在结果/输出页面中的 text/line/para 之间,如果SongNameMovieName 的值为空,那么结果/输出不应显示特定的 text/line/para 我在哪里已经把那些功能。

e.g.

例如



ABC Songis the popular Song of the XYZ Movie. The ABC Songof XYZ Movieis directed by PQR Director.

ABC SongXYZ 电影的流行歌曲。在ABC歌XYZ电影是由PQR导演执导。



Now you can see there are two sentences in the above Para. I want to put the function for the first sentence only when the field values of SongNameand MovieNameare empty then

现在你可以看到上面的Para中有两个句子。我只想在SongNameMovieName的字段值为空时才将第一句的函数放在

  • It should display the first sentence whether the field values of SongNameand MovieNameis empty or not i.e. ABC Song is the popular Song of the XYZ Movie. If the field values of SongNameand MovieNameare empty then it can leave blank space between them and I know it can be done through this function echo $_POST["SongName"];.
  • 无论SongNameMovieName的字段值是否为空,它都应该显示第一句话,即ABC Song 是 XYZ 电影的流行歌曲。如果SongNameMovieName的字段值为空,则可以在它们之间留空白,我知道可以通过此函数来完成echo $_POST["SongName"]; .

BUT

  • It should not display the first sentence line if the field values of SongNameand MovieNameare empty i.e. The ABC Song of XYZ Movie is directed by PQR Director.
  • 如果SongNameMovieName的字段值为空,即XYZ 电影的 ABC 歌曲由 PQR 导演导演,则不应显示第一句行

回答by Matthew Riches

I think what you are looking for is something like:

我认为你正在寻找的是这样的:

if(!empty($_POST['foo'])) {
   echo "a sentence".$_POST['foo']." with something in the middle.";
}

This will check that the value is NOT empty, however empty means a lot of things in PHP so you may need to be more specific. For example you may want to check simple if its set:

这将检查该值是否为空,但空在 PHP 中意味着很多东西,因此您可能需要更具体。例如,您可能想要检查 simple 是否已设置:

if(isset($_POST['foo'])) {
   echo "a sentence".$_POST['foo']." with something in the middle.";
}

回答by Almir Saraj?i?

You really confused me with the last 2-3 sentences.
What you want to do can be accomplished by using if, elseif and else control structures.

你真的让我对最后 2-3 句话感到困惑。
你想要做的事情可以通过使用 if、elseif 和 else 控制结构来完成。

if ($_POST['MovieName'] AND $_POST['SongName']) 
{
    //code
}  
elseif ($_POST['MovieName'])
{
    //code
}  
elseif ($_POST['SongName']) 
{
    //code
}  
else 
{
    //code
}

回答by mariosk89

You can do this in three ways.

您可以通过三种方式做到这一点。

  1.  

    if(isset($_POST['value1']))
    {
        //...
    }
    
  2.  

    if(!empty($_POST['value1'])) 
    {
        //...
    }
    
  3. and the simple way

    if($_POST('value1')=="")
    {
        ....
    }
    
  1.  

    if(isset($_POST['value1']))
    {
        //...
    }
    
  2.  

    if(!empty($_POST['value1'])) 
    {
        //...
    }
    
  3. 和简单的方法

    if($_POST('value1')=="")
    {
        ....
    }
    

I sometimes use the third way when i'm confused with form submitting, but of course the first and the second are more elegant!

当我对表单提交感到困惑时,我有时会使用第三种方式,但当然第一种和第二种方式更优雅!