【问题标题】:PHP SELF is automatically called on page loadPHP SELF 在页面加载时自动调用
【发布时间】:2015-08-18 17:12:32
【问题描述】:
<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $fn=$_POST["filename"];
        $content=$_POST["txt"];
        $fp= fopen("abc.txt", "w") or die("unable to open file");
        fclose($fp);
    }
?>
<html>
<head>
    <title>Files</title>
</head>
<body>
    <center>
    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" id="show">
        <input type="text" name="filename" placeholder="File Name"><br>
        <textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>
        <input id="ixtbtn" type="submit" id="tabuttton" value="Done">
    </form>
    </center>
</body>
</html>

在页面加载时会自动调用 PHP。但是我需要在按下提交按钮时调用它。

【问题讨论】:

  • 给提交按钮一个名称属性并在你的条件下使用它。
  • 如何检查状态?
  • 对于&lt;input id="ixtbtn" type="submit" name="Submit" id="tabuttton" value="Done"&gt; 检查if(isset($_POST['Submit'])) { ... your code ... }
  • 您是否注意到标签没有突出显示..我认为有问题?

标签: php html file server


【解决方案1】:
<?php
    if (isset($_POST['submitted']))
    {
        $fn=$_POST["filename"];
        $content=$_POST["txt"];
        $fp= fopen("abc.txt", "w") or die("unable to open file");
        fclose($fp);
    }
?>
<html>
<head>
    <title>Files</title>
</head>
<body>
    <center>
    <?php echo "<form action='" . htmlspecialchars(#_SERVER['PHP_SELF']) . "' method='post' id='show'>"; ?>
        <input type="text" name="filename" placeholder="File Name"><br>
        <textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>
        <input id="ixtbtn" type="submit" id="tabuttton" value="Done" name='submitted'>
    </form>
    </center>
</body>
</html>

【讨论】:

  • 您是否注意到标签没有突出显示..我认为有问题?
  • 那是因为你在一个字符串中包含了 php 标签。你需要稍微修改你的表单行来做你想做的事情。
【解决方案2】:

稍微使用了您的代码,并注意到两件事:(1)您的语句在您的 php 代码块之外并且在直接的 html 中。 (2) 在我的系统(OSX 上的 Firefox 或 Safari)上,这意味着 php $_SERVER['PHP_SELF'] 从未被翻译回 php,它被视为文本,并且 post 命令不断发布到服务器以寻找 URL从 php 开始 如果我在 php 中使用 echo 来输出您的 HTML,则变量 ($_SERVER) 不会被替换并作为纯文本输入。因此,我首先将您的 html 放入字符串变量中以翻译变量,然后回显您的 html。将文件保存为 PHP 文件而不是 html 文件,您的代码运行良好。所以,四个净变化。

  1. 将结束 php 语句 (?>) 移到文件末尾
  2. 将 HTML 复制到 php 字符串中

    $outputString = "";
    $outputString = $outputString.'<!DOCTYPE html>';
    $outputString = $outputString.'<html><head><meta charset="utf-8"><title>Files</title>';
    $outputString = $outputString.'</head><body><center>';
    $outputString = $outputString.'<form action="'.$_SERVER['PHP_SELF'].'" method="post" id="show">';
    $outputString = $outputString.'<input type="text" name="filename" placeholder="File Name"><br>';
    $outputString = $outputString.'<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>';
    $outputString = $outputString.'<input id="ixtbtn" type="submit" id="tabuttton" name="myButton" value="Done">';
    $outputString = $outputString.'</form></center></body></html>';
    
  3. 回显字符串
  4. 另存为 .php 而不是 .html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    相关资源
    最近更新 更多