Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If $_POST is empty function

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

Here is how my form looks like.

form.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 SongName and MovieName, so I used echo $_POST["SongName"]; and echo $_POST["MovieName"]; which generates the result perfectly.

Now I want to put these values SongName and MovieName between the text/line/para in the result/output page and if the value of SongName or MovieName is empty then the result/output should not display that specific text/line/para where I have put those functions.

e.g.


ABC Song is the popular Song of the XYZ Movie. The ABC Song of XYZ Movie is directed by PQR Director.


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 SongName and MovieName are empty then

  • It should display the first sentence whether the field values of SongName and MovieName is empty or not i.e. ABC Song is the popular Song of the XYZ Movie. If the field values of SongName and MovieName are empty then it can leave blank space between them and I know it can be done through this function echo $_POST["SongName"];.

BUT

  • It should not display the first sentence line if the field values of SongName and MovieName are empty i.e. The ABC Song of XYZ Movie is directed by PQR Director.
like image 359
atif Avatar asked Jun 08 '12 12:06

atif


People also ask

Is $_ POST empty?

$_POST (and not $POST) is where PHP store all data coming from a POST request, usually submitted from a form somwhere. So, empty($_POST) will return true if nothing was submitted, and false if any variable at all, with a value different than empty, was passed.

What does the empty () function do?

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.

What is $_ POST function in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.

What is considered empty in PHP?

A variable is considered empty if it does not exist or if its value equals false .


2 Answers

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:

if(isset($_POST['foo'])) {
   echo "a sentence".$_POST['foo']." with something in the middle.";
}
like image 159
Matthew Riches Avatar answered Oct 21 '22 05:10

Matthew Riches


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.

if ($_POST['MovieName'] AND $_POST['SongName']) 
{
    //code
}  
elseif ($_POST['MovieName'])
{
    //code
}  
elseif ($_POST['SongName']) 
{
    //code
}  
else 
{
    //code
}
like image 24
Almir Sarajčić Avatar answered Oct 21 '22 03:10

Almir Sarajčić