atif
atif

Reputation: 85

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

BUT

Upvotes: 8

Views: 99513

Answers (3)

mariosk89
mariosk89

Reputation: 954

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')=="")
    {
        ....
    }
    

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

Almir Sarajčić
Almir Sarajčić

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

Matthew Riches
Matthew Riches

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

Related Questions