Forum : problem in php form
Brief description  about Online courses   join in Online courses
View manjulambigai  karthikeyan 's Profile

problem in php form

sir, I create 2 file that are
www/checkform.html
<html>
<body>

<form action="welcome.php" method="POST">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html> and

www/welcome.php
</head>

<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>

i run checkform.html and click the submit button , but the o/p is

Welcome !
You are years old.

why the php codes didn't run
pls give me the answere how to correct it
Thanking u sir
Asked by manjulambigai karthikeyan | Jul 16, 2010 |  Reply now
Replies (3)
View teacher siliconindia 's Profile
Hi manjula,

//save this as index1.html inside your root folder

<html>
<body>
<head>
<script language="javascript" type="text/javascript">
function trim(stringToTrim){
return stringToTrim.replace(/^\s+|\s+$/g,'');
}

function validateForm(){
var Vname = document.myform.fname.value;
var Vage = document.myform.age.value;
if(trim(Vname)==''){
alert("Please enter your first name");
document.myform.fname.value='';
document.myform.fname.focus();
return false;
}

if(trim(Vage)==''){
alert("Please enter your age");
document.myform.age.value='';
document.myform.age.focus();
return false;
}

if(!isNaN(Vage)==false){
alert("Please enter only numbers");
document.myform.age.value='';
document.myform.age.focus();
return false;
}

}
</script>
</head>
<form action="welcome1.php" method="POST" name="myform" onsubmit="return validateForm();">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>


//Save as welcome1.php inside your root folder

<?php
if(isset($_POST["fname"]) && !empty($_POST["fname"])){
$first_name = $_POST["fname"];
echo "Welcome <font color=red><b>".$first_name."</b></font>!<br>";
}else{
echo "Name field can not be left blank";
}


if(isset($_POST["age"]) && !empty($_POST["age"])){
$age = $_POST["age"];
echo "You are <font color=red><b>".$age."</b></font> years old";
}else{
echo "<br>Age field can not be left blank";
}
?>


Try to run this code.

Thanks
Jul 21, 2010
View manjulambigai karthikeyan 's Profile
sir Thanks for ur reply,
but ur program also gives same result. ie's


Welcome !
You are years old.

what i can do for it?
pls give me the ans

thanking u
Jul 19, 2010
View teacher siliconindia 's Profile
Hi manjula


//Save as index.html inside your root folder (C:/wamp/www/index.html)

<html>
<body>

<form action="welcome.php" method="POST">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>


//Save as welcome.php inside your root folder (C:/wamp/www/welcome.php)

<html>
<head>
</head>
<body>
<?php
if(isset($_POST["fname"]) && !empty($_POST["fname"])){ ?>
Welcome <?php echo $_POST["fname"]; ?>!<br />
<?php } if(isset($_POST["age"]) && !empty($_POST["age"])){ ?>
You are <?php echo $_POST["age"]; ?> years old.
<?php } ?>

</body>
</html>

Do the same as above and try to run the file, do revert back if you still face any problem.

Jul 19, 2010