【发布时间】:2016-03-08 15:11:26
【问题描述】:
我有相当沮丧的问题。我正在尝试将表单变量存储到 cookie 中。
<form id = "vraagStellen" action = "shoutbox.php" method = "post" class = "col-lg-4 col-md-6 col-sm-12" role="form">
<a name = "bottomOfPage"></a>
<div class = "selectAfbeelding form-group">
<label for "gamertag">Gamertag:</label>
<input type = "text" id = "gamertag" name = "gamertag" class = "form-control" maxlength="30" value="<?php if(isset($_COOKIE['gamertag'])){echo $_COOKIE['gamertag'];} else {echo "";} ?>" placeholder="<?php if(isset($_COOKIE['gamertag'])){echo $_COOKIE['gamertag'];} else {echo "Hier typen";} ?>" required>
</div>
<div class = "selectVraag form-group">
<label for "bericht">Bericht:</label>
<textarea id = "bericht" onkeyup = "count()" name = "bericht" class = "form-control" maxlength="100" cols = "40" rows = "2" placeholder = "Hier typen" required></textarea>
<div id = "countDiv"></div>
</div>
<div class = "form-group">
<input type="submit" name = "verzend" value = "Verzenden" id = "verzenden">
<input type="reset" name = "reset" value = "Leegmaken" id = "reset">
</div>
</form>
还有 php:
if (isset($_POST['gamertag']) && isset($_POST['bericht']) && !empty($_POST['gamertag']) && !empty($_POST['bericht'])){
$afbeelding_before = $_POST['gamertag'];
$vraag_before = $_POST['bericht'];
$cookie_var = $_POST['gamertag'];
$afbeelding = mysql_real_escape_string($afbeelding_before);
$vraag = mysql_real_escape_string($vraag_before);
setcookie("gamertag", $cookie_var, time() + (86400 * 30), "/",$_SERVER['SERVER_NAME']);
//echo $afbeelding . $vraag;
$sql3="INSERT INTO `shoutbox` (`id`, `gamertag`, `bericht`, `date`) VALUES (NULL, '$afbeelding', '$vraag', CURRENT_TIMESTAMP)";
mysql_query($sql3,$con);
echo "post gamertag ".$_POST['gamertag']."<BR>";
echo "cookie ". $_COOKIE['gamertag']."<BR>";
header('Location: http://link.nl#shoutbox');
}
else{
echo 'Er ging iets fout...';
}
从 PHP 代码重定向时,cookie 未显示在名为“gamertag”的表单输入中。但是当我从链接中删除#shoutbox 时,它会正确显示cookie。浏览不同的页面不是问题。仅当从 PHP 代码重定向时,它才不会显示。
希望有人能帮帮我!
谢谢!
【问题讨论】: