How to Call PHP Script from a PHP Script?

How to Call PHP Script from a PHP Script?

  • PHP
  • 4 mins read

In this tutorial, you will learn how to call a PHP script from a PHP script. In the first PHP script, it will ask the user to enter username and password, and if it is correct, then it will open another PHP script for a user registration form.

How to Define a Password Field?

In PHP <input type="password"> defines a password field where a user can enter a password and password shows in asterisks ("*") or in dots (".") on the screen. So, the user can not read.

Now we are writing a PHP program based on a password field and a text field, this program is containing two conditions(the inputted value for first text box is "Steve", inputted value for password field is "Italy") and based on these conditions, a PHP file is opening otherwise this program is showing an error message.

After submitting data for the first file, we are getting the second file; here we are passing the details as "John" for the 1st text field, "Smith" for the 2nd text field, "6789011223" for the 3rd text field and here "[email protected]" for last text field.

Calling PHP Script From a PHP Script Example

The following example demonstrates, how to call a PHP script from a PHP script.

File1:

<html>
    <body>
        <form action="" method="post">
            <center>
                <table border=0>
                    <tr>
                        <td>
                            User Name
                        </td>
                        <td>
                            <input type=text name="t1">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Password
                        </td>
                        <td>
                            <input type=password name="t2">
                        </td>
                    </tr>
                </table>
                <br>
                <br>
                <input type=submit name="s" value="Result">
            </center>
            <?php
if(isset($_POST['s']))
{
    $a=$_POST['t1']; //accessing value from 1st text box
    $a1=$_POST['t2']; //accessing value from 2nd text field
    if($a=="Steve"&&$a1=="Italy")
    {
        echo "<script>"; //is defining a client-side script(JavaScript)
        echo "window.open('e1.php')"; 
        echo "</script>";
    }
    else
    {
        echo "<h1>Sorry either wrong user name or wrong password</h1>";// here <h1>is the largest font header tag
    } 
}
            ?>
        </form>
    </body>
</html>

Output:

PHP password entry example.

If username and password are correct then it will open the following file for the registration form.

e1.php file:

<html>
    <body>
        <form action="" method="post">
            <center>
                <h1>
                    Registration Form
                </h1>
                <table border=0>
                    <tr>
                        <td>
                            First Name
                        </td>
                        <td>
                            <input type=text name="t1">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Last Name
                        </td>
                        <td>
                            <input type=text name="t2">
                        </td>
                    </tr>
                    <td>
                        Phone number
                    </td>
                    <td>
                        <input type=text name="t3">
                    </td>
                </tr>
                <td>
                    Email Id
                </td>
                <td>
                    <input type=text name="t4">
                </td>
            </tr>
        </table>
        <br>
        <br>
        <input type=submit name="s" value="Result">
    </center>
    <?php
if(isset($_POST['s']))
{
    $a=$_POST['t1']; //accessing value from 1st text box
    $a1=$_POST['t2']; //accessing value from 2nd text field
    $a2=$_POST['t3']; //accessing value from 3rd text field
    $a3=$_POST['t4']; //accessing value from 4th text field
    $a4=$a." ".$a1;
    echo "<center><font size=4>Employee information are:-</font></center><br>";
    echo "<center><font size=4>Name:-</font>".$a4."</center><br>";
    echo "<center><font size=4>Phone number:-</font>".$a2."</center><br>";
    echo "<center><font size=4>Email:-</font>".$a3."</center><br>";
}
    ?>
</form>
</body>
</html>

Output

PHP registration form example.Employee information are:-
Name:-John Smith
Phone number:-892882992
Email:[email protected]

In the above program, the first file is containing a method (window.open()) which is using to open the new browser window (here e1.php file). In the second file (e1.php), we are using four text fields, and a submit button. After clicking on the submit button, we are getting the values from the input fields defined in the e1.php file, and we are displaying the result through echo statements.