我正在编写一个简单的表单验证脚本,但我遇到的一个问题是,当我通过 Google Chrome 运行我的脚本时,我的整个 php 标签显示在输入框中。在这种情况下,我的“名字”代码将<?php echo $fn; ?>
在输入框中。我试图将我的 php 变量 $fn、$ln 设置为 null,但这似乎并没有解决问题。有什么建议么?
这是我的代码:
<html>
<head>
<title> My Webpage! </title>
</head>
<body>
<?php
include 'connect_inc.php';
include 'validate.php';
$fn = null;
$ln = null;
$age = null;
$birthday = null;
if(isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['age']) && isset($_POST['birthday']))
{
$fn = $_POST['firstname'];
$ln = $_POST['lastname'];
$age = $_POST['age'];
$birthday = $_POST['birthday'];
if(!empty($fn) && !empty($ln) && !empty($age) && !empty($birthday))
{
$sec = mysql_real_escape_string;
$query = "INSERT INTO `ppl` VALUES('','".$sec($fn)."','".$sec($ln)."','".$sec($age)."','".$sec($birthday)."')";
if($query_run = mysql_query($query))
{
header('Location: formSuccess.php');
}
else
{
echo 'Unable to register at this time';
}
}
else
{
echo 'Please make sure that all the fields are filled';
}
}
?>
<form action = "form.php" method = "POST">
Firstname: <br> <input type = "text" name = "firstname" maxlength = "40" value = "<?php echo $fn;?>" > <br><br>
Lastname: <br> <input type = "text" name = "lastname" maxlength = "40"> <br><br>
Age: <br> <input type = "text" name = "age" maxlength = "3"> <br><br>
Birthday: <br><input type = "text" name = "birthday" maxlength = "13"> <br><br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>