7

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.
1
  • You can check if $_POST['key'] is set with isset($_POST['key']) and then do whatever you like.
    – Leri
    Jun 8, 2012 at 12:44

3 Answers 3

18

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.";
}
6
  • I already tried if(isset($_POST['foo'])) { but it was not working.
    – atif
    Jun 8, 2012 at 12:52
  • if(!empty($_POST['foo'])) { it seems to work what I wanted.
    – atif
    Jun 8, 2012 at 12:53
  • Just checking, you did replace foo with your actual variable name yes? Jun 8, 2012 at 12:53
  • if(!empty($_POST['foo'])) { echo "a sentence".$_POST['foo']." with something in the middle."; } this worked for me. Thanks I wanted the same.
    – atif
    Jun 8, 2012 at 12:56
  • I replaced "foo" with the variable SongName and it is working exactly I wanted.
    – atif
    Jun 8, 2012 at 13:01
2

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
}
2
  • 1
    empty() is safer than evaluating as a boolean, since the implicit isset() prevents the E_NOTICE from being raised if variable is completely undefined.
    – DaveRandom
    Jun 8, 2012 at 12:56
  • if(!empty($_POST['foo'])) { echo "a sentence".$_POST['foo']." with something in the middle."; } this worked for me. Thanks I wanted the same.
    – atif
    Jun 8, 2012 at 12:58
0

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!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.