dongqi8114 2016-02-03 17:49
浏览 26

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

So, I have a script like this:

<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>

I just want to ask if it's possible to split <?php ?> and html form in to two seperate files. I mean php part is in script.php, and the html form is in the index.php, but still they execute each other, but I still want to keep <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> instead of <form method="post" action="script.php">

  • 写回答

1条回答 默认 最新

  • dqyhj2014 2016-02-03 21:05
    关注

    Look into using a templating system, e.g. smarty http://www.smarty.net or twig http://twig.sensiolabs.org/

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀