2 回答
TA贡献1775条经验 获得超11个赞
不得在Cookie行(如echo,print_r()和HTML标记)之前输出
您可以在任何输出之前编写cookie代码
<?php
$name = $_GET['fname'];
$model = $_GET['model'];
//write cookies for name and model for 1 day
setcookie('Name', $name, time() + 86400);
setcookie('Model', $model, time() + 86400);
?>
<form action="order01.php" class="formLayout">
<div class="formGroup">
<label>First name:</label>
<input type="text" name="fname" class="textbox" autofocus required placeholder="First name" title="first name" maxlength="20" pattern="[A-Za-z'-]{2,20}">
</div>
<div class="formGroup">
<label> Car model:</label>
<div class="formElements">
<input type="radio" name="model" required value="Mustang">Ford Mustang<br>
<input type="radio" name="model" required value="Subaru">Subaru WRX STI<br>
<input type="radio" name="model" required value="Corvette">Corvette<br>
</div>
<input type="submit" value="submit">
</div>
</form>
TA贡献1946条经验 获得超3个赞
您的代码中几乎没有问题,首先,没有结束</form>标记,其次,您必须将表单提交到order01.php保存位置,cookie因此请尝试这样做,它将起作用
Order01.php
<form action="order01.php" class="formLayout">
<div class="formGroup">
<label>First name:</label>
<input type="text" name="fname" class="textbox" autofocus
required placeholder="First name" title="first name"
maxlength="20" pattern="[A-Za-z'-]{2,20}">
</div>
<div class="formGroup">
<label> Car model:</label>
<div class="formElements">
<input type="radio" name="model" required value="Mustang">Ford Mustang<br>
<input type="radio" name="model" required value="Subaru">Subaru WRX STI<br>
<input type="radio" name="model" required value="Corvette">Corvette<br>
</div>
<input type="submit" value="submit">
</div>
</form>
Order02.php
<?php
$name = $_GET['fname'];
$model = $_GET['model'];
//write cookies for name and model for 1 day
setcookie('Name', $name, time() + 86400);
setcookie('Model', $model, time() + 86400);
if(isset($_COOKIE['Name'])){
echo "Cookie ".$_COOKIE['Name']." is set";
}
else{
echo "<div class='pageContainer'>";
echo "<h2 class='containerText, centerText'>Failed to validate inputs";
echo "<br><br>";
echo "<a href='order01.php'><button>Go Back</button></a>";
echo "</div>";
exit();
}
?>
- 2 回答
- 0 关注
- 122 浏览
添加回答
举报