PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 15, 2022

[FIXED] How to handle multiple php forms using one php page

 July 15, 2022     forms, html, php, web-deployment     No comments   

Issue

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>

Solution

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().



Answered By - Twisty
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing