Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 页面刷新提交我的表单_Php_Submit_Page Refresh - Fatal编程技术网

Php 页面刷新提交我的表单

Php 页面刷新提交我的表单,php,submit,page-refresh,Php,Submit,Page Refresh,您的页面仍保留$\u POST值。所以,如果你刷新它。它将再次打印文件 因此,添加标题,如下重定向用户: <?php $rs = mysql_query("SELECT * FROM users WHERE id='$_SESSION[user_id]'"); while ($row= mysql_fetch_array($rs)) { $starter= $row['id']; $user_name= $row['user_name']; } $starterID

您的页面仍保留
$\u POST
值。所以,如果你刷新它。它将再次打印文件

因此,添加
标题
,如下重定向用户:

<?php
$rs = mysql_query("SELECT * FROM users WHERE id='$_SESSION[user_id]'");
while ($row= mysql_fetch_array($rs))
{
     $starter= $row['id'];
     $user_name= $row['user_name'];
}


$starterID=$starter;
$companyID=$_GET['id'];


$input = $_POST['message'];


date_default_timezone_set('Europe/Helsinki');
$timestamp = date('h:i', time());


$file = $companyID." and ".$starterID.".txt";


if (file_exists($file)) {
    $file = $companyID." and ".$starterID.".txt";
} else {
    $file = $starterID." and ".$companyID.".txt";
}


$current = file_get_contents($file);

$current.= "<b>$user_name</b> <br> $input $timestamp\n<br>";

if(isset($_POST['message'])){
file_put_contents($file, $current);
}

echo $current;
?>

<form action="" method="post">
<input type="text" name="message" autocomplete="off">
<input type="submit">
</form>

我尝试了所有的建议,但不幸的是那些对我不起作用

我做了更多的研究,发现对于这个案例和我来说,最好的解决方案是:

原始代码:

if(isset($_POST['message'])){
    file_put_contents($file, $current);
    header('Location:Your_File_name.php')
}
解决方案:

if(isset($_POST['message']) && $_POST != null){
file_put_contents($file, $current);
}
if(isset($\u POST['message'])&&&$\u POST!=null){
文件内容($file,$current);
回声'
location.reload();
';
}

感谢您的建议,他们给了我一个更好的搜索方法:)

您可以查看最后一条消息是否相同。如果相同,则不执行任何操作。每次刷新时,都会发布以前的内容,因此您的
If(isset($\u POST['message'))
会执行并存储内容。。在脚本的最后一部分使用@Apul Gupta的建议
unset($\u POST)
。请不要使用
mysql.*
。而是使用或使用。
if(isset($_POST['message']) && $_POST != null){
file_put_contents($file, $current);
echo' <script type="text/javascript">
location.reload();
</script>';
}