-4

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

<?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.";

 ?>
4

2 回答 2

1

表格消失是因为你exit()在写任何东西之前。

于 2013-06-24T15:42:34.823 回答
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.";

     ?>
于 2013-06-24T15:45:08.687 回答