I am having some trouble putting together a page that validates and stores user information for the cms section of my e-commerce related website.
Part of the page contains an option for the user to select if the require a billing address that differs from their shipping address. This is one of two forms on the page.
The first form is a table where the user selects if they require an alternate billing address.
Based on this choice the second form is generated either containing an additional space for the billing address if required, or not.
Here is the html relating to the first form to put things into context:
<form name="isSameAsPostal" action="" method="post">
<table class="adminTable" align="center">
<thead class="tableAddEdit">
<tr>
<th colspan="4">Details</th>
<th colspan="1">Active</th>
</tr>
</thead>
<tfoot class="tableFooter">
<tr>
<td colspan="5">This table stores the users addittional information.</td>
</tr>
</tfoot>
<tbody>
<tr>
<td colspan="4">User has different billing address?</td>
'.$topCont.'
</tr>
</tbody>
</table>
</form>
As you can see, the input its self is not contained within the form, it is applied as a variable which is set in the scripts controller.
Here is the php for that to put things into context:
if (isset($_POST['sameAsPostal']) && $_POST['sameAsPostal'] == 1)
{
$_SESSION['sameAsPostal'] = 1;
$topCont = '<td colspan="1"> <input type="checkbox" name="sameAsPostal" id="sameAsPostal" value="0" checked="checked" onChange="javascript:document.isSameAsPostal.submit();" title="Tick if user has alternate billing information." /> </td>';
$otherCont = 'postal';
}
else if (isset($_POST['sameAsPostal']) && $_POST['sameAsPostal'] == 0)
{
$_SESSION['sameAsPostal'] = 0;
$topCont = '<td colspan="1"> <input type="checkbox" name="sameAsPostal" id="sameAsPostal" value="1" onChange="javascript:document.isSameAsPostal.submit();" title="Tick if user has alternate billing information." /> </td>';
$otherCont = 'postal and billing';
}
else
{
$_SESSION['sameAsPostal'] = 0;
$topCont = '<td colspan="1"> <input type="checkbox" name="sameAsPostal" id="sameAsPostal" value="1" onChange="javascript:document.isSameAsPostal.submit();" title="Tick if user has alternate billing information." /> </td>';
$otherCont = 'postal and billing';
}
As you can see with this, javascript is used to submit the form automatically as the state of the input is changed.
Now based on this, everything works almost correctly as I had in mind when I originally started with this.
According to the controller, the checkbox is ticked, the session variable $sameAsPostal is set to ‘1’ which is used to trigger the generation of the extra billing option within the second form.
Along with this the value of the checkbox becomes ‘0’ thus allowing for the checkbox to be deselected and the billing information in the second form is removed as $sameAsPostal will be set to 0 as seen in the second part of the controller script. The last part of the controller is included as the first instance of the input being added to the page. Without it there would not be an input.
So great, and like I said this all works. But the problem that I am facing now comes with the next logical addition to this. When the second form is validated and it comes across an input error, the page is refreshed with the correct error highlighting applied. Within this, the checkbox is reset too, causing the billing information section to disappear. (the information that populates the individual inputs is still stored but the actual html of it is gone as the checkbox has been reset)
So I have tried this addition to the controller:
if (isset($_POST['sameAsPostal']) && $_POST['sameAsPostal'] == 1)
{
$_SESSION['sameAsPostal'] = 1;
$topCont = '<td colspan="1"> <input type="checkbox" name="sameAsPostal" id="sameAsPostal" value="0" checked="checked" onChange="javascript:document.isSameAsPostal.submit();" title="Tick if user has alternate billing information." /> </td>';
$otherCont = 'postal';
}
else if (isset($_POST['sameAsPostal']) && $_POST['sameAsPostal'] == 0)
{
$_SESSION['sameAsPostal'] = 0;
$topCont = '<td colspan="1"> <input type="checkbox" name="sameAsPostal" id="sameAsPostal" value="1" onChange="javascript:document.isSameAsPostal.submit();" title="Tick if user has alternate billing information." /> </td>';
$otherCont = 'postal and billing';
}
else if (isset($_SESSION['sameAsPostal']) && $_SESSION['sameAsPostal'] == 1)
{
$topCont = '<td colspan="1"> <input type="checkbox" name="sameAsPostal" id="sameAsPostal" value="0" checked="checked" onChange="javascript:document.isSameAsPostal.submit();" title="Tick if user has alternate billing information." /> </td>';
$otherCont = 'postal';
}
else if ((isset($_SESSION['sameAsPostal']) && $_SESSION['sameAsPostal'] == 1) && (isset($_POST['sameAsPostal']) && $_POST['sameAsPostal'] == 0))
{
$_SESSION['sameAsPostal'] = 0;
$topCont = '<td colspan="1"> <input type="checkbox" name="sameAsPostal" id="sameAsPostal" value="1" onChange="javascript:document.isSameAsPostal.submit();" title="Tick if user has alternate billing information." /> </td>';
$otherCont = 'postal and billing';
}
else
{
$_SESSION['sameAsPostal'] = 0;
$topCont = '<td colspan="1"> <input type="checkbox" name="sameAsPostal" id="sameAsPostal" value="1" onChange="javascript:document.isSameAsPostal.submit();" title="Tick if user has alternate billing information." /> </td>';
$otherCont = 'postal and billing';
}
And very many others to be honest… But it seems that my logic and the actual logic involved in this task seem to differ.
This addition to the controller makes the checkbox unable to be unchecked if checked.
I added the session variables in order to catch the state if the checkbox, then firstly apply the correct input to the first form and secondly generate the billng information section if needed.
But it seems no matter how I configure the controller script I cannot seem to get it to work out that way.
Could provide some information regarding or make a suggestion relating to where I am going wrong with this?
Thanks in advance for taking the time to read through this!!
If you want to use
$_SESSIONvariables, you’ll need to make sure you’re starting the session every time, to keep those values store. I’m going to assume that you’re storing the checkbox value after the form is submitted for the first time getting it from the$_POSTarray.You could just check that value from the submitted form instead, but to keep it tied to your question all I can think of is that your not starting the session correctly.
By the way, you only need to check it like this, since even if it’s not set it won’t break your code. I think you’re overcomplicating things a little.
So to summarize, make sure you
session_start()the first thing in your code, and get the value (0,1) from the form submitted. To just mark a checkbox depending on the value of the variable you could do something simpler like this:Note that if your sending the form with AJAX and the page never actually reloads, then of course PHP doesn’t do anything else since it’s executed only once, before the user gets to see the actual page. Maybe you’re assuming that a change in a variable client-side would be reflected but it won’t unless you re-execute that server-side code (refresh the page).