($ _SERVER [" PHP_SELF"])将脚本和html拆分成单独的文件,是否可能?

时间:2016-02-03 17:49:35

标签: php html

所以,我有一个这样的脚本:

<html>
<body>

<?php
//paleidziami kintamieji
$name = $email = $website = $comment = $gender = "";
$nameErr = $emailErr = $genderErr = $websiteErr = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(empty($_POST["name"])){
    $nameErr = "Iveskite varda";
    } else {  
    $name = test_input($_POST["name"]);    
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "Only letters and white space allowed"; 
}
    }

    if(empty($_POST["email"])){
        $emailErr = "Iveskite el.pasta";
    } else {
    $email = test_input($_POST["email"]);
    }

    if(empty($_POST["website"])){
        $websiteErr = "Iveskite svetaine";
    } else {
    $website = test_input($_POST["website"]);
    }

    if(empty($_POST["comment"])){
    $comment = "";
    } else {
        $comment = test_input($_POST["comment"]);
    }

    if(empty($_POST["gender"])){
        $genderErr = "Pamirsote pazymeti savo lyty";
    } else {
    $gender = test_input($_POST["comment"]);
}    
}

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


?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $_POST["name"] ?>">
<span class="error">* <?php echo $nameErr; ?></span><br>
E-Mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr; ?></span><br>
Website: <input type="text" name="website">
<span class="error">* <?php echo $websiteErr; ?></span><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>
    <br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr; ?></span><br>
<input type="submit">
</form>

</body>
</html>

我只想问一下是否可以将<?php ?>和html表单分成两个单独的文件。我的意思是php部分在script.php中,而html表单在index.php中,但它们仍然相互执行,但我仍然希望保留<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">而不是<form method="post" action="script.php">

1 个答案:

答案 0 :(得分:-2)

研究使用模板系统,例如:聪明的http://www.smarty.net或树枝http://twig.sensiolabs.org/