0% found this document useful (0 votes)
42 views8 pages

Question#1 Dispaly Message in PHP

The document contains 6 questions that demonstrate different PHP concepts: 1) Two methods for displaying messages in PHP 2) Creating a form and outputting submitted values 3) Connecting a simple form to a database and inserting form data 4) Transferring data between PHP pages with sessions 5) Taking two numeric inputs from users and displaying their sum 6) Various PHP form handling techniques like isset() and $_POST are demonstrated.

Uploaded by

Ali raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views8 pages

Question#1 Dispaly Message in PHP

The document contains 6 questions that demonstrate different PHP concepts: 1) Two methods for displaying messages in PHP 2) Creating a form and outputting submitted values 3) Connecting a simple form to a database and inserting form data 4) Transferring data between PHP pages with sessions 5) Taking two numeric inputs from users and displaying their sum 6) Various PHP form handling techniques like isset() and $_POST are demonstrated.

Uploaded by

Ali raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

Question#1 Dispaly message in php

// method 1

echo "Message from PHP Method 1<br>";

// method 2

print "Message from PHP Method 2"

?>

Question#2 Make a form in php and fill it.

<?php

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

$fname = $_POST['firstName'];

$lname = $_POST['lastName'];

$email = $_POST['email'];

$password = $_POST['password'];

echo "<h2>Out Put</h2>";

echo "First name : $fname <br>";

echo "First name : $lname <br>";

echo "First name : $email <br>";

echo "First name : $password <br><br>";

?>

<!DOCTYPE html>

<html lang="en">
<head>

<meta charset="UTF-8">

<title>Form</title>

</head>

<body>

<form method="post">

<label>First Name</label>

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

<label>Last Name</label>

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

<label>Email*</label>

<input type="email" name="email" required=""><br>

<label>Password*</label>

<input type="password" name="password" required=""><br>

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

</form>

</body>

</html>

Question#3 Connect simple form to databse

<?php

// Database Connection

$host = "localhost";
$db_name = "test";

$pass = "";

$user = "root";

$dsn = "mysql:host=$host;dbname=$db_name;charset=utf8";

try {

$conn = new PDO($dsn, $user, $pass);

// echo "Connection established";

} catch (PDOException $e) {

exit("Sorry!!! Something went wrong.");

// form insert query

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

echo "<h2>Out Put</h2>";

$fname = $_POST['firstName'];

$lname = $_POST['lastName'];

$email = $_POST['email'];

$password = $_POST['password'];

$sql = "INSERT INTO `test_tbl` (`first_name`, `second_name`, `email`,


`password`) VALUES ('$fname', '$lname', '$email', '$password');";

$query = $conn->exec($sql);

if($query){

echo "Data Inserted Successfully";


header("Refresh:1");

}else{

echo "Something Went Wrong!!!";

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Form</title>

</head>

<body>

<form method="post">

<label>First Name</label>

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

<label>Last Name</label>

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

<label>Email*</label>

<input type="email" name="email" required=""><br>

<label>Password*</label>
<input type="password" name="password" required=""><br>

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

</form>

</body>

</html>

Question#4#5

Transfer data from one php form to other form or page (or)

Transfer data from one php page to other form or page


<?php

session_start();

// send data with out form

$_SESSION['text'] = "Lorem ipsum dolor sit amet, consectetur adipisicing


elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam,";

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Transfer Data</title>

</head>

<body>
<form method="post" action="action.php">

<textarea name="text" placeholder="Some


Thing"></textarea><br>

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

</form>

</body>

</html>

action.php

<?php

session_start();

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

$text = $_POST['text'];

echo "<h3>Data Transfer From form To Other Page or


form</h3>";

echo "$text<br>";

echo "<h3>Data Transfer From One Page To Other


Page</h3>";

echo $_SESSION['text'];

?>

Question#6 Take Two input From users and show their sums
<?php

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

$num1 = $_POST['num1'];

$num2 = $_POST['num2'];

$sum = $num1+$num2;

echo "Sum is $sum";

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Sum</title>

</head>

<body>

<form method="post">

<label>First Value</label>

<input type="number" name="num1"><br>

<label>Second Value</label>

<input type="number" name="num2"><br>


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

</form>

</body>

</html>

You might also like