简体   繁体   English

将td数据传递给隐藏的输入,然后使用PHP发布

[英]passing td data to a hidden input and then posting with PHP

I have a form that I have shortended for simplicty but the concept is the same. 我已经简化了表格,但概念是相同的。

<form method=post action= somephp.php>
<table>
<tr><td><input name= first></tr>// These input are where people enter 
<tr><td><input name= second></tr>// numbers. They then click on
<tr><td><input name= third></tr>//calculate and the results appear
<tr><td><input name= fourth></tr>//in the below table
<td>Result one <input type = "hidden" name="somename" value=""></td>// 
<td>Result two <input type = "hidden" name="somename1  value=""></td>  
<button>Calculate</button>
<button type submit>save</button>
</form>

The form works fine . 表格工作正常。 The maths for the form is performed using javascript. 表格的数学运算是使用javascript执行的。 I get the values of the results in the result one and two as expected . 我按预期在结果1和2中得到结果的值。

Now for my problem After I have clicked calculate and got my results I click on the save button and want it to post all of the inputs including the hidden ones to a PHP script. 现在解决我的问题单击“计算”并得到结果后,单击“保存”按钮,希望它将所有输入(包括隐藏的输入)发布到PHP脚本中。

I am having problems posting the hidden feilds. 我在张贴隐藏的字段时遇到问题。

It works if I place a figure in the value attribute but what I want and have been struggling all night with is how can I post the result one and two feilds to my PHP script via the hidden feild. 如果我在value属性中放置一个图形,但我一直想要整夜挣扎的是如何通过隐藏的字段将结果一个和两个字段发布到我的PHP脚本中,那么它会起作用。

I have been trying in vain to get the variable from my js script to act as the value for the hidden feild but keep getting undefined index error in PHP. 我一直在徒劳地尝试从我的js脚本中获取变量,以充当隐藏字段的值,但一直在PHP中获取未定义的索引错误。 I think it may be because the page has already loaded. 我认为可能是因为页面已经加载。 I have attached the variable that holds the value for result one and result two to onChange event etc but can not get this to work. 我已经将保存结果一和结果二的值的变量附加到onChange事件等中,但是无法使其正常工作。

As a summary - how can I pass a td inner HTML (result 1 and result 2) to hidden inputs value for posting to a PHP script. 总结-如何将td内部HTML(结果1和结果2)传递给隐藏的输入值,以发布到PHP脚本。 I am open to other solutions 我愿意接受其他解决方案

Thanks in advance for your help. 在此先感谢您的帮助。

<script>

var a = document.getElementByID('result one id').value;
var b = document.getElementByID('HIDDEN INPUT ID').value; 

I want a = b 我想要a = b

but am struggling 但正在挣扎

You could consider using jquery to set values of the hidden fields. 您可以考虑使用jquery设置隐藏字段的值。

<form method=post action= somephp.php>
<table>
    <tr><td><input name= "first" /></td></tr>
    <tr><td><input name= "second" /></td></tr>
    <tr><td><input name= "third" /></td></tr>
    <tr><td><input name= "fourth" /></td></tr>
    <td>Result one <input id="hidden1"  type = "text" name="somename" value=""/></td> 
    <td>Result two <input id="hidden2" type = "text" name="somename1"  value=""/></td>
<button id="calculate">Calculate</button>
<button id="submit" type="submit">save</button>
    </table>
</form>

And the jquery 和jQuery

    $(document).on('click', '#calculate', function(){ //you could perform your calculations here or define a separate function
      $('#hidden1').val('calculated value1');
      $('#hidden2').val('calculated value2');
      return false;
    });

FIDDLE 小提琴

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

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