PHP: ASSIGNMENT NO.:5

PRACTICE PROGRAM:

3. Write a PHP script to check mobile number entered by the user is valid or not and display an appropriate message.

ANS:

HTML CODE:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <fieldset>

        <form action="Q3.php" method="POST">

            <label for="num"><b>Enter Mobile No.:</b></label>

            <input type="number" name="num" id="num"><br>

            <br>

            <input type="submit" name="submit" id="submit" value="submit">

            <input type="reset" name="reset" id="reset" value="reset">

        </form>

    </fieldset>

</body>

</html>

PHP CODE:

<?php 

error_reporting(0);

if($_SERVER["REQUEST_METHOD"]=="POST")

{

    if(isset($_POST["submit"]))

    {

        $num=$_POST["num"];

        $len=strlen($num);

        if($len<10||$len>10)

        {

            echo"<script>alert('Enter valid mobile number!');</script>";

        }else{

            echo"Number is: ".$num;

        }

    }

}

?>

-------------------------------------------------------------------------------------------------------------------

 SET A:

1. Write a PHP script to accept font name, background color, and welcome message on 1st page. Display the welcome message with the given font and background color on the next page.

ANS:

HTML CODE:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <h1>Change font,bg color of given text!</h1>

    <fieldset>

    <form action="#" method="POST">

        <label for="font_name"><b>Enter font name:</b></label>

        <input type="text" name="font_name"><br>

        <br>

        <label for="bg_color"><b>Enter background color:</b></label>

        <input type="text" name="bg_color"><br>

        <br>

        <label for="text"><b>Enter any sentence or text:</b></label>

        <input type="text" name="text"><br>

        <br>

        <input type="submit" name="submit" id="submit" value="submit">

        <input type="reset" name="reset" id="reset" value="reset">

    </form>

</fieldset>

</body>

</html>

PHP CODE:

<?php 

error_reporting(0);

if($_SERVER["REQUEST_METHOD"]=="POST")

{

    $font=$_POST["font_name"];

    $bg=$_POST["bg_color"];

    $text=$_POST["text"];

    echo"<body style='background-color:$bg'>";

    echo"<h2 style='font-style:$font'>$text</h2>";

}

?>

---------------------------------------------------------------------------------------------------------------------------
2. Write a PHP program to accept name, address, pincode, gender information. If any field is blank display error messages “all fields are required”.

ANS:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Student details!</h1>
    <fieldset>
    <form action="#" method="POST">
        <label for="name"><b>Enter your name:</b></label>
        <input type="text" name="name" id="name"><br>
        <br>
        <label for="address1"><b>Enter your address:</b></label>
        <input type="text" name="address1" id="address1"><br>
        <br>
        <label for="pin"><b>Enter your pin-code:</b></label>
        <input type="number" name="pin" id="pin"><br>
        <br>
        <label for="gender"><b>Select Gender:</b></label>
        <input type="radio" name="gender" id="gender" value="male">male
        <input type="radio" name="gender" id="gender" value="female">female <br>
        <br>
        <input type="submit" name="submit" id="submit" value="submit">
        <input type="reset" name="reset" id="reset" value="reset">
    </form>
</fieldset>
</body>
</html>

PHP CODE:
<?php 
error_reporting(0);
if($_SERVER["REQUEST_METHOD"]=="POST")
{
    if(isset($_POST["submit"]))
    {
        $name=$_POST["name"];
        $adr=$_POST["address1"];
        $pin=$_POST["pin"];
        $gender=$_POST["gender"];
        if($name==""||$adr==""||$pin==""||$gender=="")
        {
            echo"<script>alert('All fields are required');</script>";
        }else{
            echo"<b>";
            echo"The name is:".$name."<br>";
            echo"The address is:".$adr."<br>";
            echo"The pin code is:".$pin."<br>";
            echo"The Gender is:".$gender."<br>";
            echo"</b>";
        }
    }
}

?>
 ----------------------------------------------------------------------------------------------------------------------
3. Write a PHP script to accept employee details (name, address) and earning details (basic, DA, HRA). Display employee details and earning details in the proper format.
ANS:

HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Employee Details!</h1>
    <fieldset>
        <form action="#" method="POST">
            <label for="name"><b>Enter your name:</b></label>
            <input type="text" name="name" id="name"><br>
            <br>
            <label for="address1"><b>Enter your address:</b></label>
            <input type="text" name="address1" ><br>
            <br>
            <label for="earning"><b>Select Earning:</b></label>
            <select name="earning" id="earning">
                <option value="basic">basic</option>
                <option value="DA">DA</option>
                <option value="HRA">HRA</option>
            </select>   <br>
            <br>
         <input type="submit" name="submit" id="submit" value="submit">
            <input type="reset" name="reset" id="reset" value="reset">
        </form>
    </fieldset>
</body>
</html>

PHP CODE:
<?php 
error_reporting(0);
if($_SERVER["REQUEST_METHOD"]=="POST")
{
    if(isset($_POST["submit"]))
    {
        $name=$_POST["name"];
        $adr=$_POST["address1"];
        $earn=$_POST["earning"];
        echo"<table border='1px solid black' cellspacing='5px' cellpadding='5px'>";
        echo"<tr>";
        echo"<th>name</th>";
        echo"<th>address</th>";
        echo"<th>Earning</th>";
        echo"</tr>";
        echo"<tr>";
        echo"<td>$name</td>";
        echo"<td>$adr</td>";
        echo"<td>$earn</td>";
        echo"</tr>";
        echo"</table>";
    }
}


?>
----------------------------------------------------------------------------------------------------------------------------
SET B:
1. Write a PHP script to accept customer name and the list of product and quantity on the first page. On the next page display the name of the customer, name of the products, rate of the product, quantity, and total price in table format

ANS:

HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1> Customer Details!</h1>
    <fieldset>
        <form action="Q1.php" method="POST">
            <label for="name"><b>Enter your Name:</b></label>
            <input type="text" name="name" id="name"><br>
            <br>
            <label for="list"><b>Select products:</b></label>
            <select name="list[]" multiple="multiple">
                <option value="sugar">sugar</option>
                <option value="corn">corn</option>
                <option value="tea powder">tea powder</option>
                <option value="cookies">cookies</option>
            </select><br>
            <br>
            <label for="sugar"><b>Enter Quantity for sugar(kg):</b></label>
           <input type="number" name="sugar" id="sugar"><br>
           <br>
           <label for="corn"><b>Enter Quantity for corn(kg):</b></label>
           <input type="number" name="corn" id="corn"><br>
           <br>
           <label for="tea"><b>Enter Quantity for tea powder(kg):</b></label>
           <input type="number" name="tea" id="tea"><br>
           <br>
           <label for="cookies"><b>Enter Quantity for cookies(kg):</b></label>
           <input type="number" name="cookies" id="cookies"><br>
           <br>
           <input type="submit" name="submit" id="submit" value="submit">
           <input type="reset" name="reset" id="reset" value="reset">
        </form>
    </fieldset>
</body>
</html>

PHP CODE:
<center>
<?php 
error_reporting(0);
if($_SERVER["REQUEST_METHOD"]=="POST")
{
    if(isset($_POST["submit"]))
    {
        $name=$_POST["name"];
        $l=$_POST["list"];
        $sugar=$_POST["sugar"];
        $corn=$_POST["corn"];
        $cook=$_POST["cookies"];
        $t=$_POST["tea"];
        $s=35;
        $tea=100;
        $c=75;
        $co=80;
        $list=array();
        $i=0;
        foreach ($l as $k) {
           $list[$i]=$k;
           $i++;
        }
       
        $i=0;
        while($i<count($list))
        {
        if($list[$i]=="sugar")
        {
            $ps=$list[$i];
            $rs=$sugar*$s;
        }else if($list[$i]=="corn"){
            $pc=$list[$i];
            $rc=$corn*$c;
        }else if($list[$i]=="cookies")
        {
            $pco=$list[$i];
            $rco=$co*$cook;
        }else if($list[$i]=="tea powder"){
            $ptp=$list[$i];
            $rt=$tea*$t;
        }
        $i++;
    }
    $total=$rs+$rc+$rco+$rt;
    echo"<h1>customer purchase details!</h1>";
    echo"<table border='1px solid black' text-align='center'>
    <tr>
    <th>customer name</th>
    <th>product name</th>
    <th>rate of product</th>
    <th>quantity</th>
    <th>total cost(per/product)</th>
    <th>total</th>
    </tr>";
    echo"<tr>
    <td rowspan='4'>$name</td>";
    echo"<td>$ps</td>";
    echo"<td>$s</td>";
    echo" <td>$sugar</td>";
    echo"<td>$rs</td>";
    echo"<td>$total</td>";
    echo"</tr>";
    echo"<tr>";
    echo"<td>$pc</td>";
    echo"<td>$c</td>";
    echo" <td>$corn</td>";
    echo"<td>$rc</td>";
    echo"<tr>";
    echo" <td>$ptp</td>";
    echo" <td>$tea</td>";
    echo" <td>$t</td>";
    echo"<td>$rt</td>";
    echo"</tr>";
    echo"<tr>";
    echo"<td>$pco</td>";
    echo"<td>$co</td>";
    echo" <td>$cook</td>";
    echo"<td>$rco</td>";
    echo"</tr>";
    echo"</table>";
    }
}
?>
</center>
-----------------------------------------------------------------------------------------------------------------
2. Write HTML code to design multiple choice question paper for PHP subject. Display question wise marks and total marks received by the student in table format.
ANS:

HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript">
        function submit()
        {
            alert("Response submitted!");
        }
    </script>
</head>
<body>
    <h1>PHP MCQ Questions</h1>
    <fieldset>
    <form action="Q2.php" method="POST">
        <label for="Q1"><b>What is php?</b></label><br>
        <input type="checkbox" name="Q1[]" id="Q1" value="scripting language"><b>scripting language</b><br>
        <input type="checkbox" name="Q1[]" id="Q1" value="programming language"><b>programming language</b><br>
        <input type="checkbox" name="Q1[]" id="Q1" value="server side scripting language"><b>server side scripting language</b><br>
        <input type="checkbox" name="Q1[]" id="Q1" value="client side scripting language"><b>client side scripting language</b><br>
        <br>
        <label for="Q2"><b>Which sign is use in php for defining a variable?</b></label><br>
        <input type="checkbox" name="Q2[]" id="Q2" value="$"><b>$</b><br>
        <input type="checkbox" name="Q2[]" id="Q2" value="#"><b>#</b><br>
        <input type="checkbox" name="Q2[]" id="Q2" value="&"><b>&</b><br>
        <input type="checkbox" name="Q2[]" id="Q2" value="%"><b>%</b><br>
        <br>
        <label for="Q3"><b>which letters are used for formatting a date in php?</b></label><br>
        <input type="checkbox" name="Q3[]" id="Q3" value="m"><b>m</b><br>
        <input type="checkbox" name="Q3[]" id="Q3" value="d"><b>d</b><br>
        <input type="checkbox" name="Q3[]" id="Q3" value="y"><b>y</b><br>
        <input type="checkbox" name="Q3[]" id="Q3" value="f"><b>f</b><br>
        <br>
        <label for="Q4"><b>which functions are used for validating email in php?</b></label><br>
        <input type="checkbox" name="Q4[]" id="Q4" value="filter_var"><b>filter_var()</b><br>
        <input type="checkbox" name="Q4[]" id="Q4" value="preg_match"><b>preg_match()</b><br>
        <input type="checkbox" name="Q4[]" id="Q4" value="date"><b>date()</b><br>
        <input type="checkbox" name="Q4[]" id="Q4" value="array_pop"><b>array_pop()</b><br>
        <br>
        <label for="Q5"><b>Which extension are use to create php script?</b></label><br>
        <input type="checkbox" name="Q5[]" id="Q5" value="php"><b>php</b><br>
        <input type="checkbox" name="Q5[]" id="Q5" value="js"><b>js</b><br>
        <input type="checkbox" name="Q5[]" id="Q5" value="html"><b>html</b><br>
        <input type="checkbox" name="Q5[]" id="Q5" value="java"><b>java</b><br>
        <br>
        <input type="submit" name="submit" id="submit" value="submit" onclick="submit()">
        <input type="reset" name="reset" id="reset" value="reset">
    </form>
</fieldset>
</body>
</html>

PHP CODE:

<?php 
error_reporting(0);
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(isset($_POST["submit"]))
{
    $Q1=array();
    $a=$_POST["Q1"];
    $i=0;
    foreach($a as $k)
    {
        $Q1[$i]=$k;
        $i++;
    }
    $Q2=array();
    $b=$_POST["Q2"];
    $i=0;
    foreach($b as $k)
    {
        $Q2[$i]=$k;
        $i++;
    }
    $Q3=array();
    $c=$_POST["Q3"];
    $i=0;
    foreach($c as $k)
    {
        $Q3[$i]=$k;
        $i++;
    }
    $Q4=array();
    $d=$_POST["Q4"];
    $i=0;
    foreach($d as $k)
    {
        $Q4[$i]=$k;
        $i++;
    }
    $Q5=array();
    $e=$_POST["Q5"];
    $i=0;
    foreach($e as $k)
    {
        $Q5[$i]=$k;
        $i++;
    }

  
    {
        if($Q1[0]=="scripting language"&&$Q1[1]=="server side scripting language")
        {
            $q1=10;
        }else if($Q1[0]=="scripting language"||$Q1[1]=="server side scripting language"){
            $q1=8;
        }else{
            $q1=0;
        }
    }
    for($i=0;$i<count($Q2);$i++)
    {
       if($Q2[$i]=="$")
       {
        $q2=10;
       }else{
        $q2=0;
       }
    }
   
    
        if($Q3[0]=="m"&&$Q3[1]=="d"&&$Q3[2]=="y")
        {
            $q3=10;
        }else if($Q3[0]=="m"&&$Q3[1]=="d"&&$Q3[2]=="y"){
            $q3=8;
        }else{
            $q3=0;
        }
    }
    {
        if($Q4[0]=="filter_var"||$Q1[1]=="preg_match")
        {
            $q4=10;
        }else{
            $q4=0;
        }
    }
    for($i=0;$i<count($Q5);$i++)
    {
    if($Q5[$i]=="php")
    {
        $q5=10;
    }
}
$total=$q1+$q2+$q3+$q4+$q5;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table{
            text-align: center;
            align-items: center;
            justify-content: center;
            margin-right: 25%;
            margin-left: 25%;
        }
        h1{
            text-align: center;
            align-items: center;
        }
    </style>

</head>
<body>
    <h1>Result!</h1>
    <table border="1px solid black">
        <tr>
            <th>Sr.No.</th>
            <th>Questions</th>
            <th>maximum marks</th>
            <th>obtained marks</th>
        </tr>
        <tr>
            <td>1</td>
            <td>What is php?</td>
            <td>10</td>
            <td><?php echo$q1;?></td>
        </tr>
        <tr>
            <td>2</td>
            <td>Which sign is use in php for defining a variable?</td>
            <td>10</td>
            <td><?php echo$q2;?></td>
        </tr>
        <tr>
            <td>3</td>
            <td>which letters are used for formatting a date in php?</td>
            <td>10</td>
            <td><?php echo$q3;?></td>
        </tr>
        <tr>
            <td>4</td>
            <td>which functions are used for validating email in php?</td>
            <td>10</td>
            <td><?php echo$q4;?></td>
        </tr>
        <tr>
            <td>5</td>
            <td>Which extension are use to create php script?</td>
            <td>10</td>
            <td><?php echo$q5;?></td>
        </tr>
        <tr>
            <th colspan="2">Total:</th>
            <td>50</td>
            <td><?php echo$total;?></td>
        </tr>
    </table>
</body>
</html> 
------------------------------------------------------------------------------------------------------------------------------------------
3. Write a PHP script to accept student name and list of programming languages (using drop down box) and display it on the next page in the proper format.

ANS:

HTML CODE:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Student Detail!</h1>
    <fieldset>
        <form action="Q3.php" method="POST">
            <label for="name"><b>Enter your name:</b></label>
            <input type="text" name="name" id="name"><br>
            <br>
            <label for="select"><b>Select languages:</b></label>
            <select name="select[]" id="select" multiple>
                <option value="C programming">C programming</option>
                <option value="HTML">HTML</option>
                <option value="javascript">javascript</option>
                <option value="PHP">PHP</option>
                <option value="C++">C++</option>
                <option value="java">java</option>
                <option value="python">python</option>
                <option value="ReactJS">ReactJS</option>
                <option value="AngularJS">AngularJS</option>
            </select><br>
            <br>
            <input type="submit" name="submit" id="submit" value="submit">
            <input type="reset" name="reset" id="reset" value="reset">
        </form>
    </fieldset>
</body>
</html>

PHP CODE:

<?php 
error_reporting(0);
if($_SERVER["REQUEST_METHOD"]=="POST")
{
    if(isset($_POST["submit"]))
    {
        $name=$_POST["name"];
        $list=$_POST["select"];
        $a=array();
        $i=0;
        foreach($list as $b)
        {
            $a[$i]=$b;
            $i++;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <center>
    <h1>Student name and subject</h1>
    <table border="1px solid black">
        <tr>
            <th>Student name</th>
            <th>Subject's</th>
        </tr>
        <tr>
            <td rowspan="<?php echo count($a);?>"><?php echo$name;?></td>
            <?php
            for($i=0;$i<count($a);$i++) 
            {
            ?>
            <td><?php echo$a[$i];?></td>
            </tr>
            <?php 
            }
            ?>
    </table>
    </center>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
4. Write a PHP script to accept user name, email address and age. If data entered by the user is valid then display it on the next page otherwise display the appropriate message(use filter_var()).

ANS:

HTML CODE:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>User Details!</h1>
    <fieldset>
        <form action="Q4.php" method="POST">
            <label for="name"><b>Enter your name:</b></label>
            <input type="text" name="name" id="name"><br>
            <br>
            <label for="e-mail"><b>Enter E-mail Address:</b></label>
            <input type="text" name="e-mail" id="e-mail"><br>
            <br>
            <label for="age"><b>Enter your Age:</b></label>
            <input type="text" name="age" id="age"><br>
            <br>
            <input type="submit" name="submit" id="submit">
            <input type="reset" name="reset" id="reset">
        </form>
    </fieldset>
</body>
</html>
 
PHP CODE:

<?php  
error_reporting(0);
if($_SERVER["REQUEST_METHOD"]=="POST")
{
    if(isset($_POST["submit"]))
    {
        $name=$_POST["name"];
        $email=$_POST["e-mail"];
        $age=$_POST["age"];
        if(filter_var($age,FILTER_VALIDATE_INT)===0||filter_var($age,FILTER_VALIDATE_INT)===false||$age=="")

        {
            echo"<script>alert('Enter valid age!');</script>";


        }else if(filter_var($email,FILTER_VALIDATE_EMAIL)===false||$email==""){

            echo"<script>alert('Enter valid email address!');</script>";

        }else if(!preg_match("/^[a-zA-Z]*$/",$name)||$name=="")
        {
            echo"<script>alert('Enter valid name!');</script>";

        }else{

            echo"The name is : ".$name."<br>";
            echo"The Email is : ".$email."<br>";
            echo"The age is : ".$age."<br>";
        }
    }
}
?>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. A web application that takes name and age from an HTML page. If the age is less than 18, it should send a page with “Hello , you are not authorized to visit the site” message, where should be replaced with the entered name. Otherwise, it should send a “Welcome to this site” message.
 
ANS:
HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Student Registration!</h1>
    <fieldset>
    <form action="Q1.php" method="POST">
        <label for="name"><b>Enter Your Name:</b></label>
        <input type="text" name="name" id="name"><br>
        <br>
        <label for="age"><b>Enter Your Age:</b></label>
        <input type="number" name="age" id="age"><br>
        <br>
        <input type="submit" name="submit" id="submit">
        <input type="reset" name="reset" id="reset">
    </form>
</fieldset>
</body>
</html>

PHP CODE:
<?php 
error_reporting(0);
if(isset($_POST["submit"]))
{
    $name=$_POST["name"];
    $age=$_POST["age"];
    if($age<18)
    {
        echo"<script>alert('Hello $name! your not authorized to visit this site!');</script>";
    }else{
        echo"<script>alert('Welcome $name to this site!');</script>";
    }
}
?>
---------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

PHP ALL ASSIGNMENT PDF

DATA STRUCTURE ALL PDF(LAB ASSIGNMENTS)

DATA STRUCTURE :ASSIGNMENT NO.8:TREE