【问题标题】:Return an array to post into the $_SESSION in PHP返回一个数组以发布到 PHP 中的 $_SESSION
【发布时间】:2014-01-06 07:57:09
【问题描述】:

我有一个使用 Checkbox 输入的表单,我将它们全部命名为数组 name="benefits[]"

这被填充到一个 jquery 中以用作链选择的一部分。

我的问题是如何将数组放入 $_SESSION 中,以便无论选中哪个复选框,我都可以在我网站内另一个页面上的另一个输入框中使用它?

<input type="checkbox" class="comOne" name="benefits[]" value="Pension credit" id="pension_credit" /> 
Pension credit 
<a href="#" class="showBenefitInfo" data-value="Pension credit is a top-up to the state pension and is a benefit that must be applied for if your state pension is below a certain amount.">[More...]</a>

抱歉,如果我不应该这样做,不确定您是否可以上传文本框,无论如何这是复选框的整个代码部分。如果需要,我可以粘贴整个表单。

                            <div class="row">
                                <div class="col-md-5">  
                                    <label class="checkbox">
                                        <input type="checkbox" class="comOne" name="benefits[]" value="Pension credit" id="pension_credit" /> Pension credit <a href="#" class="showBenefitInfo" data-value="Pension credit is a top-up to the state pension and is a benefit that must be applied for if your state pension is below a certain amount.">[More...]</a>
                                    </label>
                                </div>
                                <div class="col-md-5">  
                                    <label class="checkbox">
                                        <input type="checkbox" class="comOne" rel="CTC"  name="benefits[]" value="Child tax credit" id="child_tax_credit" /> Child tax credit <a href="#" class="showBenefitInfo" data-value="Child Tax Credit is a benefit that must be applied for, it is not the same as Child Benefit. An up-to-date Child Tax Credit award notice must be available to show the surveyor to proceed with your application. ">[More...]</a>
                                    </label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-5">  
                                    <label class="checkbox">
                                        <input type="checkbox" class="comOne" rel="ISJSA" name="benefits[]" value="Income support" id="income_support" /> Income support <a href="#" class="showBenefitInfo" data-value="Income support is a means-tested benefit and is primarily received by single parents with a child under 5.">[More...]</a>
                                    </label>
                                </div>
                                <div class="col-md-6">  
                                    <label class="checkbox">
                                        <input type="checkbox" class="comOne" rel="ISJSA" name="benefits[]" value="Job seekers allowance" id="job_seekers_allowance" /> Income based job seekers allowance <a href="#" class="showBenefitInfo" data-value="You must be in receipt of INCOME based job seekers allowance. This will be confirmed on your award notice (you will see printed on your letter: 'This assessment is based on how much the law says you need to live on').">[More...]</a>
                                    </label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-5">  
                                    <label class="checkbox">
                                        <input type="checkbox" class="comOne" rel="WTC" name="benefits[]" value="Working tax credit" id="working_tax_credit" /> Working tax credit <a href="#" class="showBenefitInfo" data-value="You will need to have you Working Tax Credit award notice available (this will be the same as Child Tax Credit award notice if both are being received). Page 2 and 3 will confirm your household income and this MUST be less than &pound;15,860 to qualify.">[More...]</a>
                                    </label>
                                </div>
                                <div class="col-md-6">  
                                    <label class="checkbox">
                                        <input type="checkbox" class="comOne" rel="ESA" name="benefits[]" value="Income related employment" id="income_related" /> Income related employment and support allowance <a href="#" class="showBenefitInfo" data-value="Also known as ESA, Employment and Support Allowance must be INCOME related and NOT contribution based. This will be confirmed on your ESA award notice.">[More...]</a>
                                    </label>
                                </div>
                            </div> 
                            <div class="row">
                                <hr>
                                    <div class="col-md-12">
                                        <label class="checkbox">
                                            <input type="checkbox" class="comOne" name="benefits[]" value="I do NOT receive any of the above benefits" id="no_benefits" /> I do NOT receive any of the above benefits 
                                        </label>
                                    </div>
                                </hr>
                            </div>

【问题讨论】:

  • 如果有多个复选框,建议使用索引来准确识别选中的复选框,即"name=benefits[&lt;id&gt;]"
  • 所以使用类似 name="benfits[1]" 等的数字或使用输入中的 id 作为示例我有“id="pension_credit" 所以使用 name=benfits"[ Pension_credit]"?当 jquery 使用好处 [] 来查看使用了哪个复选框时,还会发生什么?

标签: php arrays session session-variables


【解决方案1】:

这很简单:

$_SESSION['benefits'] = $_POST['benefits'];

【讨论】:

  • 谢谢,我在发帖前先试过了。这就是我在页面上的内容,我希望它显示在输入框中。它在我的其他页面的输入框中返回数组
  • 您是否将session_start() 放在您希望使用会话的所有页面的顶部?这是唯一会阻止它工作的事情。
  • 您应该使用两个页面的代码更新这个问题,因为这是基本的 PHP 并且应该可以工作。也许我们发现了问题。
猜你喜欢
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 2010-12-14
  • 2013-10-23
  • 2023-04-09
相关资源
最近更新 更多