如何使我的PHP代码动态化?

时间:2014-12-20 18:09:42

标签: php forms

我是php的新手,我正在研究某些形式(从W3学校学习)。我习惯于HTML和CSS,根本没有逻辑,但我正在开始一个新项目,我必须学习PHP和javascript。

<html>
<body>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="site">
<br><br>
Comment: <textarea name="comment" rows="4" cols="40"></textarea>
<br><br>
Gender: <input type="radio" name="gender" value="male">Male
        <input type="radio" name="gender" value="female">Female
<br><br>
<input type="submit" value="Submit"> 

<?php
// Must define the variables and give them no values
$name = $email = $gender = $comment = $website = "";


function secure_input($data) 
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
   $name = secure_input($_POST["name"]);
   $email = secure_input($_POST["email"]);
   $website = secure_input($_POST["site"]);
   $comment = secure_input($_POST["comment"]);
   $gender = secure_input($_POST["gender"]);
}



echo "<h1>Your Input:</h1>";
echo "Your name is: $name";
echo "<br>";
echo "Your email is $email";
echo "<br>";
echo "Your website is: $website";
echo "<br>";
echo "Comment: $comment";
echo "<br>";
echo "Your gender is: $gender";



?>
</html>
</body>

在点击输入按钮(提交)后,如何在结尾显示echo语句?

1 个答案:

答案 0 :(得分:1)

解决这个问题的一种方法:

if($_POST) {
  echo "<h1>Your Input:</h1>";
  echo "Your name is: $name";
  echo "<br>";
  echo "Your email is $email";
  echo "<br>";
  echo "Your website is: $website";
  echo "<br>";
  echo "Comment: $comment";
  echo "<br>";
  echo "Your gender is: $gender";
}

您可以执行其他检查以查看您的变量是否已设置。

我实际上会选择:

if($_POST) {
  $name = secure_input($_POST["name"]);
  $email = secure_input($_POST["email"]);
  $website = secure_input($_POST["site"]);
  $comment = secure_input($_POST["comment"]);
  $gender = secure_input($_POST["gender"]);

  echo "<h1>Your Input:</h1>";
  echo "Your name is: $name";
  echo "<br>";
  echo "Your email is $email";
  echo "<br>";
  echo "Your website is: $website";
  echo "<br>";
  echo "Comment: $comment";
  echo "<br>";
  echo "Your gender is: $gender";
}