我想要做的是,当用户单击submit时,数据被输入到数据库中,它会将我重定向到一个显示用户提交的内容的“谢谢”页面。
代码片段: html代码& php代码:
<form action="ThankYouReport.html" method="post"
enctype="multipart/form-data">
<div>
<div class="container">
<label><input
type="checkbox" name="check[]" value="Beans">Beans</label>
<label><input type="checkbox" name="check[]" value="Apple" >
Apple</label>
</div>
</form>
<?php
require('config.php');
if(isset($_POST['sub']))
{
$checkbox1=$_POST['check'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk .= $chk1.",";
}
mysql_query("INSERT INTO `list` (`checked1`) VALUES ( ','$chk')") or
die(mysql_error());
}
?> 我环顾四周,发现您必须更改表单操作,我这样做了,但由于某种原因,我的数据不会被输入到我的数据库中。
我将如何将数据显示到另一个页面。
我知道这个网站上有很多帖子,但他们都帮不了我。
发布于 2017-04-12 23:03:26
试一试,看看下面的注释:
<?php
session_start(); // always do this at the very top of your script
require('config.php');
if (isset($_POST['sub'])) {
$chk = implode(',',$_POST['check']); // shorter than using foreach
$sql = "INSERT INTO `list` (`checked1`) VALUES ('{$chk}')"; // just a way of storing a query to a variable
if(mysql_query($sql)) {
$_SESSION['success_msg'] = 'Successfully added items '.$chk; // store a message or whatever into a session variable
// FYI if you wrap a string with single quotes, you cannot use a variable within like in above query string, but have to be concatenated using a dot (.)
header('Location: http://www.example.com/ThankYouReport.html'); // redirect on success
} else {
die('Could not insert query'); // or print this message
}
}
?>
<form method="post" enctype="multipart/form-data"><!-- remove action attribute or use name of script this is in. -->
<div class="container">
<label><input type="checkbox" name="check[]" value="Beans"> Beans</label>
<label><input type="checkbox" name="check[]" value="Apple"> Apple</label>
</div>
</form>编辑(基于注释)
如果您想将数据从一个脚本传输到另一个脚本,特别是对于一些简单的内容,比如感谢页面或类似的,您可以使用php会话。您需要将“谢谢”页面转换为PHP脚本,如下所示:
thanks.php
<?php
session_start();
?>
<div>
<?php echo $_SESSION['success_msg'];?>
</div>https://stackoverflow.com/questions/43380751
复制相似问题