写表单提交到文本文件

时间:2013-06-24 15:37:36

标签: php

我有一个简单的表单,它使用表单回显提交值。我希望将这个值写入文本文件,我似乎无法弄清楚为什么我的代码没有任何想法?

<?php
if (isset($_POST['button1'])) { 
$txt=$_POST['button1']; 
file_put_contents('status.txt',$txt,FILE_APPEND|LOCK_EX); 
exit();
}
?>

    <form method="post" action="<?php echo $PHP_SELF;?>">
    Restaurant Open:
    <input type="radio" name="button1" value="Open" onClick="submit();" <?php echo       ($_POST['button1'] == 'Open') ? 'checked="checked"' : ''; ?> /> Open                            
<input type="radio" name="button1" value="Closed" onClick="submit();" <?php echo                ($_POST['button1'] == 'Closed') ? 'checked="checked"' : ''; ?>/> Closed
    </form> 

<?php

 if (isset($_POST['button1']) == 'Open')
 echo "Open Today.";

 else if (isset($_POST['button1']) == 'Closed')
 echo "Closed Today.";

 ?>

2 个答案:

答案 0 :(得分:1)

在写任何内容之前,表格会因exit()而消失。

答案 1 :(得分:0)

我建议你使用这段代码。

单选按钮上的

,我改了

<?php echo ($_POST['button1'] == 'Open') ? 'checked="checked"' : ''; ?>

进入

if($_POST['button1'] == 'Open') echo "checked=checked"; ?>

这将避免被误解。

  <?php
    if (!empty($_POST['button1'])) { 
    $txt=$_POST['button1']; 
    file_put_contents('status.txt',$txt,FILE_APPEND|LOCK_EX); 
    //removed exit
    }
    ?>

<form method="post" action="">
    Restaurant Open:
<input type="radio" name="button1" value="Open" onClick="submit();" <? if($_POST['button1'] == 'Open') echo "checked=checked"; ?> /> Open                            
<input type="radio" name="button1" value="Closed" onClick="submit();" <? if($_POST['button1'] == 'Closed') echo "checked=checked"; ?> /> Closed
</form> 

    <?php

     if ($_POST['button1'] == 'Open')
     echo "Open Today.";

     elseif ($_POST['button1'] == 'Closed')
     echo "Closed Today.";
     else
     echo "Choose a status.";

     ?>