I am having a problem with a checkbox that I need to be part of a stick form, that will be remembered via a session so that the user can navigate through different pages and still have their checkbox selection remember for when they return.
The checkbox is to be clicked by the user if they want to have their delivery details the same as the billing details they entered at the top of the form. No need to worry about how I assign the delivery details to the same as the billing details as I do that on later pages.
Note: Javascript is completely out for this, don’t think its needed anyway but just a heads up :). This is one of those sticky-forms that posts to itself untill all criteria are met($flag == "ok") and then send the user to the next page.
I will now provide what I believe to be the relevant code:
PHP (this section of code may not be relevant but I am including it as I am as can be seen, setting what the final $_SESSION['sameasbilling'] will be before I send the user to the next page(order-confirm.php)
if ($flag == "ok"){
$sameasbilling = cleanString($_POST['sameasbilling']);
$_SESSION['sameasbilling'] = $sameasbilling;
if (!headers_sent()){
//send user to next page
header("Location: order-confirm.php");
}else{
echo "Not Sent";
}
}
HTML/PHP
<p>Same as billing address?
<input type="checkbox" id="sameasbilling" name="sameasbilling" value="1"
<?php
if (isset($_POST['sameasbilling'])) {
print ' checked="checked"';
$_SESSION['sameasbilling'] = "1";
} else {
$_SESSION['sameasbilling'] = "";
}
?> />
As far as I can see the most important piece of code is above where I try and figure out whether to output checked=’checked’ or not and whether to reset/set the ‘sameasbilling’ session value.
P.S I have been over and over this loads of times and so the IF statement I have in my input element is pretty scruffy, but at the moment does remember your selection when you stay on the same page and receive errors but when you move to the next page and return your selection is not remembered and I’m aware that I do not have any code that would actually do that at all, but I’m hoping someone could suggest how I can achieve that.
This will only work for the first page when the input box is submitted. To get it to work after returning to the page you need to also check the $_SESSION variable you have set. You could try adding an or section to your if statement such as.
**Edit