简体   繁体   English

PHP-复选框输入验证-将复选框的状态存储为会话变量?

[英]PHP - Checkbox input validation - Storing state of checkbox as a session variable?

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. 我在整理一个页面来验证并存储与电子商务相关的网站的cms部分的用户信息时遇到一些麻烦。

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: 这是与将事物置于上下文中的第一种形式有关的html:

<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: 这是将内容放入上下文的php:

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. 正如您所看到的,随着输入状态的更改,javascript用于自动提交表单。

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. 根据控制器,将复选框打勾, $sameAsPostal变量$sameAsPostal设置为“ 1”,该变量用于触发第二种形式的额外计费选项的生成。

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. 与此同时,复选框的值变为'0',因此允许取消选中该复选框,并删除第二种形式的记帐信息,因为$sameAsPostal将被设置为0,如控制器脚本第二部分所示。 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) (填充各个输入的信息仍会存储,但是由于复选框已重置,所以实际的html消失了)

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 $_SESSION variables, you'll need to make sure you're starting the session every time, to keep those values store. 如果要使用$_SESSION变量,则需要确保每次都启动会话,以保留这些值。 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 $_POST array. 我将假定您是在第一次提交表单后从$_POST数组获取表单值时存储该复选框的值。

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. 我认为您使事情变得有些复杂。

if($_SESSION['sameAsPostal'] == 1){
    //Do your code for checked
}else{
    //Do your code for unchecked
}

So to summarize, make sure you session_start() the first thing in your code, and get the value (0,1) from the form submitted. 因此,总而言之,请确保您session_start()是代码中的第一件事,并从提交的表单中获取值(0,1)。 To just mark a checkbox depending on the value of the variable you could do something simpler like this: 要仅根据变量的值标记一个复选框,可以执行以下更简单的操作:

$checked = ($_SESSION['variable'] == 1)?1:0;
echo "<input type='checkbox' value='$checked' title='My checkbox' />";

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. 请注意 ,如果您发送带有AJAX的表单并且页面实际上从未被重新加载,那么PHP当然不会做任何其他事情,因为在用户看到实际页面之前,它只执行了一次。 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). 也许您假设会反映出客户端变量的更改,但是除非您重新执行该服务器端代码(刷新页面),否则它不会反映出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM