-1

我在同一页面上有一个简短的 html 表单和一些 php 代码,以便在提交表单时,条目出现在同一页面上。该代码可以将输入的信息从表单发布到页面,但我有两个问题:

  1. 出于某种原因,文本框只允许我输入 1 个字符,现在它不允许我输入任何字符。

  2. 每次我刷新页面以再次尝试表单时,信息都会不断附加。我只希望/需要它在提交后显示一次。

    <form method="post" action="">
     <label>Select Out of Office Message
    
     <select name = "selectoutofofficemessage">
      <option value = "N/A">N/A</option>
      <option value = "Vacation">Vacation</option>
      <option value = "Conference">Conference</option>
      <option value = "Meeting">Meeting</option>
      <option value = "Other">Other</option>
     </select>
    
     <label>Custom Out of Office Message
     <input type="text" name="customoutofofficemessage" size="30" maxlength="255"/>
     </label>
    
     <p>
       <input type="submit" name="submit" value="Submit" />
     </p>
    </form>
    
    <?php
     $selectoutofofficemessage = $_POST["selectoutofofficemessage"];
     $customoutofofficemessage = $_POST["customoutofofficemessage"];
     $posts = file_get_contents("posts.txt");
     $posts = "$selectoutofofficemessage - $customoutofofficemessage\n" . $posts;
     file_put_contents("posts.txt", $posts);
     echo $posts;
    ?>
    
4

3 回答 3

0
<form method="post" action="">

<label>Select Out of Office Message</label>
<select name = "selectoutofofficemessage">

 <option selected="selected" value="">Select a Message</option>

 <option value ="N/A">N/A</option>

 <option value = "Vacation">Vacation</option>

 <option value = "Conference">Conference</option>

 <option value = "Meeting">Meeting</option>

 <option value = "Other">Other</option>

</select>

<label>Custom Out of Office Message</label>

<input type="text" name="customoutofofficemessage" size="30" maxlength="255"/>



<p>

<input type="submit" name="submit" value="Submit" />

</p>

</form>


<?php

$posts = file_exists("posts.txt") ? file_get_contents("posts.txt") : "";

if(!empty($_POST))
{   
$selectoutofofficemessage = $_POST["selectoutofofficemessage"];
$customoutofofficemessage = $_POST["customoutofofficemessage"];
$posts = "$selectoutofofficemessage - $customoutofofficemessage AT  <small><em>". date('h:m A - M-d-Y') ."</em></small>  <br>" . $posts;
file_put_contents("posts.txt", $posts);
}

echo $posts;

?>

<?php
if (!empty($_POST)){
?>
<script type="text/javascript">
    window.location = window.location.href;
</script>
<?php } ?>
于 2013-11-06T18:22:03.493 回答
0

对 Post 值进行验证

if(isset(selectoutofofficemessage))

{
execute the html code;

}

别的 {

php代码....

} 那么该值将不会一次又一次地重复,不要附加它只是显示它....

于 2013-11-05T15:02:54.890 回答
0

1.第一个标签不正确,没有关闭。修理它:

<label>Select Out of Office Message
   <select>
       ...
   </select>
</label>

2.更改您的PHP代码:

<?php

$posts = file_exists("posts.txt") ? file_get_contents("posts.txt") : "";

if(!empty($_POST))
{
    $selectoutofofficemessage = $_POST["selectoutofofficemessage"];
    $customoutofofficemessage = $_POST["customoutofofficemessage"];
    $posts = "$selectoutofofficemessage - $customoutofofficemessage\n" . $posts;
    file_put_contents("posts.txt", $posts);
}

echo $posts;

考虑提交后的每次页面刷新都会导致最后一次提交。

于 2013-11-05T14:56:32.973 回答