Php: Form returns form

I have a radio button form that returns a certain type of form for each particular option. The data that i get from the second from will be used to search in a database. My problem is that if i use the post method for both forms when i submit data in the second from i get the notice that i haven't done the same for the first one because my code verifies if the variables are empty each time the page is loaded. What can i do in PHP so the server will differentiate between the forms and won't do the checking each time data is submitted through post? I tried naming the submit buttons but it didn't seem to work. The code that searches in the database it is written in another file and it is working. Thanks in advance.

<!doctype html>
<html>
<body>
<?php
// define variables and set to empty values
$optiuneErr="";
$optiune="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{

   if (empty($_POST["optiune"]))
     {$optiuneErr= "Selectati optiunea";}
   else
     {$optiune = test_input($_POST["optiune"]);}
}
?>
<?php
$numeErr=$anErr=$regiuneErr=$succesorErr=$predecesorErr="";
$nume=$an=$regiune=$succesor=$predecesor="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{

    if (empty($_POST["nume"]))
     {$numeErr= "Introduceti numele";}
   else
     {$nume = test_input($_POST["nume"]);}
     if (empty($_POST["an"]))
     {$anErr = "Introduceti anul";}
   else
     {$an = test_input($_POST["an"]);}
     if (empty($_POST["regiune"]))
     {$regiuneErr = "Introduceti regiunea";}
   else
     {$regiune = test_input($_POST["regiune"]);}
     if (empty($_POST["succesor"]))
     {$succesorErr = "Introduceti succesorul";}
   else
     {$succesor = test_input($_POST["succesor"]);}
     if (empty($_POST["predecesor"]))
     {$predecesorErr = "Introduceti predecesorul";}
   else
     {$predecesor = test_input($_POST["predecesor"]);}
}
function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>
<p><span class="error">* Selectati un camp.</span></p>
<form method="post" > 
<input type="radio" name="optiune" value="1" />Cauta dupa nume <br />
<input type="radio" name="optiune" value="2" />Cauta dupa an<br />
<input type="radio" name="optiune" value="3" />Cauta dupa regiune<br />
<input type="radio" name="optiune" value="4" />Cauta dupa succesor<br />
<input type="radio" name="optiune" value="5" />Cauta dupa predecesor<br />
<input type="submit" name="submit1" value="Alege" />
<span class="error"> <?php echo $optiuneErr;?></span>
</form>

<?php
switch($optiune)
{
    case 1:
        echo '
        <p><span class="error">* Introduceti o valoare.</span></p>
        <form method="POST">
        Nume: <input type="text" name="nume"><br>
        <input type="submit" value="Cauta" />
        <span class="error"> <?php echo $numeErr;?></span>
        </form>';
        break;
    case 2:
        echo '
        <p><span class="error">* Introduceti o valoare.</span></p>
        <form method="POST" >
        An: <input type="text" name="an"><br>
        <input type="submit" value="Cauta" />
        <span class="error"> <?php echo $anErr;?></span>
        </form>';
        break;
    case 3:
        echo '
        <p><span class="error">* Introduceti o valoare.</span></p>
        <form method="POST">
        Regiune: <input type="text" name="regiune"><br>
        <input type="submit" value="Cauta" />
        <span class="error"> <?php echo $regiuneErr;?></span>
        </form>';
        break;
    case 4:
        echo '
        <p><span class="error">* Introduceti o valoare.</span></p>
        <form method="POST">
        Succesor: <input type="text" name="succesor"><br>
        <input type="submit" value="Cauta" />
        <span class="error"> <?php echo $succesorErr;?></span>
        </form>';
        break;
    case 5:
        echo '
        <p><span class="error">* Introduceti o valoare.</span></p>
        <form method="POST">
        Predecesor: <input type="text" name="predecesor"><br>
        <input type="submit" value="Cauta" />
        <span class="error"> <?php echo $predecesorErr;?></span>
        </form>';
        break;
}
?>
</body>
</html>

Answers


You should make use of isset() function.

if (isset($_POST['submit1']))
{

if (empty($_POST["optiune"]))
 {$optiuneErr= "Selectati optiunea";}
else
 {
    if(isset($_PoST['nume']))
    {
       $nume = test_input($_POST["nume"]);
    }
    else if($_POST['an'])
    {
      ///////some checking code/////
    }

    ////and so on for all individual forms///
  }
}

like this for the second form.


Need Your Help

maxRequestLength+maxAllowedContentLength+HTTPS not working

asp.net azure iis web-applications web-config

I have made a service that i host on an azure webapp. This will be used to upload files. IIS has a built in security feature that limits the file upload size.

Mapping COM VARIANT Types to actual types using a map

c++ com map variant

I am writing a COM wrapper for a COM object that sends different types of values from a client and want to map these types in a Map to their actual C++ type, such as VT_BSTR to a wstring, etc.