简体   繁体   English

隐藏输入值到PHP会话

[英]Hidden input value to php session

I have a form, which uses the following input towards the end: 我有一个表单,该表单在最后使用以下输入:

<input type="hidden" id="ct_count" name="ct_count" value=""/>

The initialisation of the form is: 表格的初始化为:

<form action="email_submission.php" method="post" id="form1" onsubmit="mySubmit();">

And the mySubmit function is: mySubmit函数是:

function mySubmit() {
  document.getElementById('ct_count').value = ct;
  document.getElementById("form1").submit();
}

When i hit submit, I want to pass the value of ct, which is a variable count on the page, to email_submission.php and store it in a session variable. 当我点击Submit时,我想将ct的值(这是页面上的变量计数)传递到email_submission.php并将其存储在会话变量中。 The session variable is returning blank on every submit, and i'm unsure if the value of the "ct" variable used is being passed through on the hidden field. 会话变量在每次提交时都返回空白,我不确定所使用的“ ct”变量的值是否正在隐藏字段中传递。

Is someone able to pick up where i'm going wrong? 有人可以接我在哪里吗? There are already variables stored correctly through this, so it's not my session settings as far as i know. 通过此方法已经正确存储了变量,据我所知这不是我的会话设置。

tldr: What's the correct way to take a javascript variable count and pass it through form submit to php? tldr:进行JavaScript变量计数并将其通过表单提交给php的正确方法是什么?

EDIT: this is the code for adding fields. 编辑:这是添加字段的代码。 intiial loop for the variable "ct" 变量“ ct”的初始循环

function new_link()
{
ct++;
<?php $ct = $ct + 1; ?>

document.getElementById("sec4_lender").setAttribute('name', 'sec4_lender_<?php echo $ct;?>');
document.getElementById("sec4_balance").setAttribute('name', 'sec4_balance_<?php echo $ct;?>');
document.getElementById("sec4_termdate").setAttribute('name', 'sec4_lender_<?php echo $ct;?>');
document.getElementById("sec4_security").setAttribute('name', 'sec4_security_<?php echo $ct;?>');
document.getElementById("sec4_description").setAttribute('name', 'sec4_description_<?php echo $ct;?>');
document.getElementById("sec4_status").setAttribute('name', 'sec4_status_<?php echo $ct;?>');
document.getElementById("sec4_repayment").setAttribute('name', 'sec4_repayment_<?php echo $ct;?>');
document.getElementById("sec4_repayment2").setAttribute('name', 'sec4_repayment_2_<?php echo $ct;?>');

var div1 = document.createElement('tr');
div1.id = 'sect4busloan_div_'+ct+'';
// link to delete extended form elements
var delLink = '<tr style="text-align:right;margin-right:65px"><a href="javascript:delIt('+ ct +')">Del</a></tr>';
div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
document.getElementById('newlink').appendChild(div1);

}

// function to delete the newly added set of elements
function delIt(eleId)
{
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('newlink');
parentEle.removeChild(parentEle.childNodes[eleId]);
var newct = ct - 1;
ct = newct;
<?php $ct = $ct - 1;?>

}

You're taking the input's value and always setting it to ct - 您要获取输入值,并始终将其设置为ct

document.getElementById('ct_count').value = ct;

You should get a warning or error about ct in the console if you do it this. 如果这样做,您应该在控制台中收到有关ct的警告或错误。 What you should do is set a variable to the value of the input - 您应该做的是将变量设置为输入的值-

var ct = document.getElementById('ct_count').value; 

It also appears that you never set the value of the hidden field, so in the example I post here ct 's value will always be blank. 似乎也从未设置过隐藏字段的值,因此在我在此处发布的示例中, ct的值将始终为空白。

It turned out that while bug testing i had moved that single session out of the conditionals for my script. 事实证明,在进行错误测试时,我已经将单个会话移出了脚本的条件语句。 So for every page other than the first i was moving on to, it was requesting a blank variable. 因此,对于除我要浏览的第一页以外的所有页面,它都请求一个空白变量。

Idiocy at its finest. 最好的白痴。 Thank you very much for everyone helping in this matter. 非常感谢您在此问题上所提供的帮助。

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

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