So I want this element on a webpage to be replaced with something different and stay that way even if they go to a different page. It's PHP code that takes inputs, sets a cookie based on that input and then should read the cookie to determine what the reloaded page looks like. After the user puts something into the email or phone number boxes it should display a "thank you for subscribing" message and then if the user wants to input a new number/ email they can click a button to return the page to normal, but you have to hit either button twice to get the preferred result.
if($_COOKIE["booleanSubmit"] === "1" || !isset($_COOKIE["booleanSubmit"])){$Newsletter = <<<OUTPUT
<p >Thanks for signing up for our updates!<p><form method="post">
<form>
<input type="submit" name="reset" value="Sign Up Again" />
</form>
OUTPUT;
} else{
$Newsletter = <<<OUTPUT
<section>
<div >
<h2>Keep up with David's!</h2>
<p>Stay all up in our grill for exclusive offers, discounts, menu updates, and more.</p>
<form action="index.php" method="post">
<label for="email">Email address</label><input type="text" id="email" name="email" placeholder="Email address">
<label for="phoneNumber">Phone number</label><input type="text" id="phoneNumber" name="phoneNumber" placeholder="Phone number">
<input type="submit" name="newsletter_signup" value="Sign Up">
</form>
</div>
</section>
OUTPUT;
}
if (isset($_POST["email"]) && !empty($_POST["email"])){
setcookie("booleanSubmit", 1, time() (86400 * 30));
$_COOKIE['booleanSubmit'] = 1;
}elseif (isset($_POST["phoneNumber"]) && !empty($_POST["phoneNumber"])){
setcookie("booleanSubmit", 1, time() (86400 * 30));
$_COOKIE['booleanSubmit'] = 1;
}
if (isset($_POST["reset"])){
setcookie("booleanSubmit", 0, time() (86400 * 30));
$_COOKIE['booleanSubmit'] = 0;}
CodePudding user response:
Have you tried simply swapping the cookie setting part to before the output part? Like so? Also no need to strict compare the cookie value.
if (isset($_POST["email"]) && !empty($_POST["email"])) {
setcookie("booleanSubmit", 1, time() (86400 * 30));
$_COOKIE['booleanSubmit'] = 1;
} elseif (isset($_POST["phoneNumber"]) && !empty($_POST["phoneNumber"])) {
setcookie("booleanSubmit", 1, time() (86400 * 30));
$_COOKIE['booleanSubmit'] = 1;
}
if (isset($_POST["reset"])) {
setcookie("booleanSubmit", 0, time() (86400 * 30));
$_COOKIE['booleanSubmit'] = 0;
}
if (isset($_COOKIE["booleanSubmit"]) && intval($_COOKIE["booleanSubmit"]) == 1) {
$Newsletter = <<<OUTPUT
<p >Thanks for signing up for our updates!<p><form method="post">
<form>
<input type="submit" name="reset" value="Sign Up Again" />
</form>
OUTPUT;
} else {
$Newsletter = <<<OUTPUT
<section>
<div >
<h2>Keep up with David's!</h2>
<p>Stay all up in our grill for exclusive offers, discounts, menu updates, and more.</p>
<form action="index.php" method="post">
<label for="email">Email address</label><input type="text" id="email" name="email" placeholder="Email address">
<label for="phoneNumber">Phone number</label><input type="text" id="phoneNumber" name="phoneNumber" placeholder="Phone number">
<input type="submit" name="newsletter_signup" value="Sign Up">
</form>
</div>
</section>
OUTPUT;
}
CodePudding user response:
Figured it out. scrapped it and began again. IDK what the issue was but here's the new code.
if (isset($_POST["email"]) && !empty($_POST["email"])){
setcookie("booleanSubmit", $_POST["email"], time() 14400);
$_COOKIE['booleanSubmit'] = $_POST["email"];
}
elseif (isset($_POST["phoneNumber"]) && !empty($_POST["phoneNumber"])){
setcookie("booleanSubmit", $_POST["phoneNumber"], time() 14400);
$_COOKIE['booleanSubmit'] = $_POST["phoneNumber"];
}
else if (isset($_POST["reset"])){
setcookie("booleanSubmit", "", time() - 3600);
$_COOKIE['booleanSubmit'] = "";
}
if (isset($_COOKIE["booleanSubmit"]) && !empty($_COOKIE["booleanSubmit"])){
$Newsletter =
<<<OUTPUT
<p >Thanks for signing up for our updates!<p><form method="post">
<form>
<input type="submit" name="reset" value="Sign Up Again" />
</form>
OUTPUT;
} else {
$Newsletter = <<<OUTPUT
<section>
<div >
<h2>Keep up with David's!</h2>
<p>Stay all up in our grill for exclusive offers, discounts, menu updates, and more.</p>
<form action="index.php" method="post">
<label for="email">Email address</label><input type="text" id="email" name="email" placeholder="Email address">
<label for="phoneNumber">Phone number</label><input type="text" id="phoneNumber" name="phoneNumber" placeholder="Phone number">
<input type="submit" name="newsletter_signup" value="Sign Up">
</form>
</div>
</section>
OUTPUT;
}