Reputation: 85
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
Upvotes: 8
Views: 99513
Reputation: 954
You can do this in three ways.
if(isset($_POST['value1']))
{
//...
}
if(!empty($_POST['value1']))
{
//...
}
and the simple way
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!
Upvotes: 0
Reputation: 1570
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
}
Upvotes: 3
Reputation: 2286
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.";
}
Upvotes: 18