0

I'm a student and I know nothing about PHP. but I have to do one of my assignment using PHP.

Here is the problem which I faced. On my index page, there are 3 links that direct to 3 different forms. when the user chooses one form, then fill and submit it result.php file shows the output using values that the user enters in the form. all the 3 forms should germinate its result using the same result.php file.

I cannot figure out how to generate the result page by identifying which form the user selects. Here is my code,

form1.php

<!DOCTYPE html>
<html>
<head>
    <title>PHP form handling</title>
</head>
<body>
    <form name="form1" action="result.php" method="post">
        <label for="pullDownMenu">Title</label> 
        <select name="pullDownMenu" id="pullDownMenu" size="1"> 
            <option value="Mr">Mr</option> 
            <option value="Ms">Ms</option> 
            <option value="Mrs">Mrs</option> 
            <option value="Rev">Rev</option>
        </select>

        <p>Name: <input type="text" name="firstname" value="" /></p>
        <p>Reg No: <input type="text" name="lastname" value="" /></p>
        <p>Email Addr: <input type="text" name="Email" value="" /></p>

        <input type="submit" name="submit" value="Submit" />
    </form>
</body>
</html>

form2.php

<!DOCTYPE html>
<html>
<head>
    <title>form 2</title>
</head>
<body>
    <form name="form2" action="result.php" method="post">
        <p>Registrationa no: <input type="text" name="RegNO" value="" /></p>
        <p>NIC number <input type="text" name="NIC" value=""></p>
        <p>Telephone number: <input type="text" name="Telephone" value="" /></p>
        <input type="submit" name="submit" value="Submit" />
    </form>
</body>
</html>

I tried the result.php file, but it didn't work. here is the result.php

<!DOCTYPE html>
<html>
<head>
    <title>PHP demo</title>
</head>
<body>
    <?php
        if(!empty($_POST['form1'])){
            $title=$_POST['pullDownMenu'];
            $firstname = $_POST['firstname'];
            $lastname = $_POST['lastname'];
            $Email = $_POST['Email'];

            echo"<h1>student information</h1>";
            echo'title is : ' . $title . '</br>';
            echo 'first name is : '. $firstname . '</br>';
            echo 'lastname is : '.$lastname;
        }

        if (!empty($_POST['form2'])) {
            $regNo = $_POST['RegNO'];
            $NIC = $_POST['NIC'];
            $tel = $_POST['Telephone'];

            echo "<p>Following details are saved to database</p>";
            echo 'reg No\t:\t' . $regNo. '</br>';
            echo 'NIC\t:\t' . $NIC. '</br>';
            echo 'Tel No\t:\t' . $tel. '</br>';
        }
    ?>
</body>
</html>
4
  • You can have different values for the submit button and use that to trigger the respective form handler Commented Jan 6, 2020 at 2:53
  • #Swetank Poddar how can I identify different form values inside result.php file Commented Jan 6, 2020 at 2:57
  • Consider iterating over all the indexes in $_POST[] and echo'ing them. If you need very specialized results, you can look at the $_SERVER[] and identify which form submitted the details and then customize the results. Commented Jan 6, 2020 at 2:57
  • $_POST["submit"] == "something" Commented Jan 6, 2020 at 3:00

2 Answers 2

3

Consider using isset() to check for a specific variable. It can be better then checking with empty().

<!DOCTYPE html>
<html>
<head>
  <title>PHP demo</title>
</head>
<body>
<?php 
if(isset($_POST['form1'])){
  echo "<h1>student information</h1>\r\n";
  echo "title is : $_POST['pullDownMenu']<br />\r\n";
  echo "first name is : $_POST['firstname']<br />\r\n";
  echo "lastname is : $_POST['lastname']\r\n";
}
if (isset($_POST['form2'])) {
  echo "<p>Following details are saved to database</p>\r\n";
  echo "reg No\t:\t$_POST['RegNO']<br />\r\n";
  echo "NIC\t:\t$_POST['NIC']<br />\r\n";
  echo "Tel No\t:\t$_POST['Telephone']<br />\r\n";
}
?>
</body>
</html>

If you have more forms, consider using switch() instead of if().

Sign up to request clarification or add additional context in comments.

Comments

1

You could set different values for button submit for each form. Or you could use a input tag hidden to set type of form. Example:

Form1 : <input type="hidden" name="type" value="form1">
Form2 : <input type="hidden" name="type" value="form2">

And in php form

if($_POST["type"]=="form1")
{
//code here
}else if($_POST["type"]=="form2"){
//code here
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.