【问题标题】:HTML PHP form not working [duplicate]HTML PHP表单不起作用[重复]
【发布时间】:2018-03-27 21:34:46
【问题描述】:

我制作了一个表格,它在另一个名为 data.php 的文件中添加了一些行。 代码如下:

<?php
if (isset($_POST['post'])){

    // GET EMAIL
    $post_text = $_POST["post_text"];
    if($post_text == '') $error = "Post content is empty";
    $filename = getcwd() . "data.php"; 
    $line_i_am_looking_for = 1; 
    $lines = file( $filename , FILE_IGNORE_NEW_LINES ); 
    $lines[$line_i_am_looking_for] = $post_text; 
    file_put_contents( $filename , implode( "\n", $lines ) );
    ?>

    <html>
    <head>
    </header
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <?php
            if ($error) {
            ?>

                <script type="text/javascript">
                    // close the errordiv in secs
                    window.setTimeout("close();", 4000);

                    function close(){
                        document.getElementById("errordiv").style.display="none";
                    }
                </script>

            <?php   
            }   
            ?>
            <div id="errordiv" style="display:block; color:#FF3535">
                <b><?php if($error) echo $error; ?></b>
            </div>
            Email:
            <input name="post_text" size="30" type="text">

            <input class="button" value="Submit" type="submit" name="post">
        </form> 
    </body>
</html>

当我打开index.php,在文本框中输入一些单词并提交表单时,我想将输入的文本写入data.php文件的第二行。 但它不起作用。

【问题讨论】:

  • 这段代码有效:
  • 但我想将表单数据添加到 data.php 文件中。不是像“我修改的 line2”这样的文本。

标签: php html php-7 fwrite php-5.3


【解决方案1】:

我正在研究代码,我自己找到了答案。 我的代码中缺少 }。编辑后的代码在这里:

<?php
if (isset($_POST['post'])){

// GET EMAIL
        $post_text = $_POST["post_text"];
if($post_text == '') $error = "Post content is empty";
$filename = getcwd() . "/data.php"; 
$line_i_am_looking_for = 1; 
$lines = file( $filename , FILE_IGNORE_NEW_LINES ); 
$lines[$line_i_am_looking_for] = $post_text; 
file_put_contents( $filename , implode( "\n", $lines ) );
}
?>

<html>
<head>
</header
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if($error){
?>

<script type="text/javascript">
// close the errordiv in secs
window.setTimeout("close();", 4000);

function close(){
document.getElementById("errordiv").style.display="none";
}
</script>

<?php   
}   
?>
<div id="errordiv" style="display:block; color:#FF3535"><b><?php if($error) echo $error; ?></b></div>
Email:
<input name="post_text" size="30" type="text">

<input class="button" value="Submit" type="submit" name="post">
</form> 


</body>
</html>

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多