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
BUT
$_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.
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.
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.
A variable is considered empty if it does not exist or if its value equals false .
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.";
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With