1.

Create a form to accept the name of the user as input as shown.If the user leaves the inputbox blank then it should display error as shownand if user enters name then it should display as shown below

Answer»

<html>

<head>

<style>

.error{color:#FF0000}

</style>

</head>

<body>

<?php

$name = "";

$nameErr = "";

$flag=0;

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

{

if (empty($_POST["name"]))

{

$nameErr = "Name is required";

}

else

{

$flag=1;

}

}

function test_input($data)

{

$data = trim($data);

$data = stripslashes($data);

$data = htmlspecialchars($data);

return $data;

}

?>

<form action="<? php echo $_SERVER[‘PHP_SELF’];?>" method="post">

Name: <input type="text" name="name"><span class="error">* </span>

<?php echo $nameErr;?><br>

<input type="submit" value="submit"><br>

<?php

if($flag==1)

{

echo "Hello".test_input($_POST["name"]);

}

?>

</form>

</body>

</html>



Discussion

No Comment Found

Related InterviewSolutions