1

我是一个试图在网站上完成一些功能的菜鸟。不幸的是,我的授权很少,所以我想尽可能简单地创建一个评论框,而不使用数据库或 jQuery、JavaScript。

我做了很多搜索,我相信最简单的方法是创建一个带有评论表单的类似日志的 HTML,PHP 脚本会将输入的文本附加到该表单上。这是我迄今为止设法制造的:

$file = "updates.html";
$fh = fopen($file, 'a');
$file = "updates.html";
$fh = fopen($file, 'a');
$comment = echo $_POST["update"] \n";
fwrite($fh, $comment);
fclose($fh);

updates.html 文件有一个注释框,其中的操作指向具有上述内容的 php 文件。当然,它不起作用,存在解析错误,但我不知道如何在那里使用变量(如果这是问题的原因)。我只是不知道该怎么做......你有什么建议吗?谢谢!

4

5 回答 5

2

您打开文件两次。您无需回显您的 $_POST['update]

<?php 
$file = "updates.html"; 
$fh = fopen($file, 'a'); 
$comment = $_POST["update"] . "\n"; 
fwrite($fh, $comment); 
fclose($fh); 
?>
于 2013-06-11T11:41:30.990 回答
2

不知道你想做什么......

 <?php
  if(isset($_POST['update'])) {
     // if a request update exists
     $file = "updates.html";
     file_put_contents($file, $_POST['update']."\n",FILE_APPEND);
  }
  ?>
于 2013-06-11T11:42:10.483 回答
1

这个答案适用于将来可能有类似问题的任何人。

尽管在没有数据库的情况下添加评论是不切实际的,但它是可行的。下面是如何去做。

第 1 步:创建一个文件并将其保存为 .php 扩展名,如 comment.php 第 2 步。创建通常的 html 表单并将表单方法 =“post”和表单操作设置为文件名,如 action =“comment.php”。 php"

<h3> Add a comment here </h3>

<form action="comment.php" method="post">
<label for="name">Name:</label>
<input type="text" name="yourname"><br>
<label for="name">Comment:</label> 
<textarea name="comment" id="comment" cols="30" rows="10"></textarea>
<input type="submit" value="submit">
</form>

` 步骤 3. 在同一文件 comment.php 中编写一个 php 脚本来处理来自表单的数据。请记住将脚本包含在 php 标记中

<?php

$yourname = $_POST['yourname'];
$comment = $_POST['comment'];

// format the comment data into how you want it to be displayed on the page
$data = $yourname . "<br>" . $comment . "<br><br>";

//Open a text file for writing and save it in a variable of your chosen.    
//Remember to use "a" not "w" to indicate write. Using 'w' will overwrite 
// any existing item in the file whenever a new item is written to it.

$myfile = fopen("comment.txt", "a"); 

//write the formatted data into the opened file and close it
fwrite($myfile, $data); 
fclose($myfile);

// Reopen the file for reading, echo the content and close the file
$myfile = fopen("comment.txt", "r");
echo fread($myfile,filesize("comment.txt")); 

?>              
于 2016-08-16T01:53:24.833 回答
0

将其放入文件中可能会导致问题,因为您需要某种分隔符,您可以使用 base64 对其进行编码并在条目末尾附加 \n

 $input = base64_encode(htmlspecialchars($_POST['update'])); //consider using strip_tags as well to avoid injections

 file_put_contents("updates.html", $input."\n");

获取条目使用

 $entires = file("updates.html");
 if(count($entries) > 0)
 {
  foreach($entries as $entry)
  {
   echo base64_decode($entry);
  }
 }
 else
 {
   echo 'no entries so far';
 }

如果您不想使用 Db,则应该考虑至少使用 SimpleXml 。

于 2013-06-11T11:46:05.293 回答
0

必填项目

"; } else { $v_firstname = ""; } if ($lastname=="") { $v_lastname= "Required Field
"; } else { $v_lastname= ""; } if ($password=="") { $v_password= "Required Field
"; } else { $v_password= ""; } if ($password!=$passwordRetype) { $v_passwordRetype= "Password did not match!
"; } else { $v_passwordRetype= ""; } if ($gender=="") { $v_gender= "Required Field
"; } else { $v_gender= ""; } if ($student_id=="") { $v_student_id= "Required Field
"; } else { $v_student_id= ""; } if ($firstname!="" && $lastname!= "" && $password == $passwordRetype && $student_id!= "" && $email!= "" && $gender!= ""){ $checkme=mysql_query("SELECT * FROM members WHERE student_id = '$student_id'") or die(mysql_error()); $checkmyid=mysql_numrows($checkme); if($checkmyid > 0){ header("location:checkid.php"); }else{ mysql_query("INSERT INTO members (firstname, lastname, password,url, gender, student_id, status_id,photo,account_status) VALUES ('$firstname','$lastname','$password','$email','$gender','$student_id','0','default.jpg','0')")or die(mysql_error()); $wewness = mysql_query("SELECT * FROM members WHERE student_id = $student_id")or die(mysql_error()); $getid = mysql_fetch_array($wewness); $_SESSION['member_id'] = $getid['memberid']; $_SESSION['login'] = 'true'; $_SESSION['studentid'] = $student_id; header("location:registerexec.php"); } } } if(isset($_POST['login'])){ $studentid = $_POST['studid']; $pass = $_POST['password']; $query2 = mysql_query("SELECT * FROM members WHERE student_id = '$studentid' AND password = '$pass' ") or die (mysql_error()); while($studid = mysql_fetch_object($query2)) { echo "$studid->member_id"; } $numberOfRows = MYSQL_NUMROWS($query2); if ($numberOfRows == 0) { } else if ($numberOfRows > 0){ $wewness = mysql_query("SELECT * FROM members WHERE student_id = $studentid")or die(mysql_error()); $getid = mysql_fetch_array($wewness); if($getid['account_status']==0){ $_SESSION['login'] = 'maybe'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:registerexec.php'); }elseif($getid['account_status']==2){ $_SESSION['login'] = 'true'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:hometest.php'); }elseif($getid['account_status']==1){ $_SESSION['login'] = 'maybe'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:fill.php'); } } } ?> 

通过格伦

于 2013-07-12T07:28:12.890 回答