通过表单发送多个按钮信息 [英] Send multiple button information through form

查看:114
本文介绍了通过表单发送多个按钮信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个按钮和值的表单。当我按下提交按钮和值时,我想提交哪个按钮被点击。我在下面的代码中的问题与PHP循环。该代码显示每个问题的是和否按钮。我正在使用jquery保持类活动并切换它们(这工作正常)。我的问题是当我提交表单时,只有通过POST提交问题,并且没有按下按钮的数据。

  echo'<按钮类型应该如何处理这个按钮才能让提交按钮被按下(所有按钮都有唯一的ID)? =buttonname =yes。$ value [Key]。'id =answerf1class =lietruth yes>是< / button> 

生成页面的HTML

 < div id =Answertoggle16> 
< div id =questions-box>< input type =hiddenname =question16value =Question1>< input type =hiddenname =keyvalue =5>< input type =hiddenname =uservalue =truthsandlie>< button type =buttonname =yes5id =answerf1class =lietruth yes >是< / button>< button type =buttonname =no5id =answer1class =lietruth no>否< / button>
< div id =questions> Question1< / div>
< / div>
< / div>
< div id =Answertoggle17>
< div id =questions-box>< input type =hiddenname =question17value =Question2>< input type =hiddenname =keyvalue =9>< input type =hiddenname =uservalue =truthsandlie>< button type =buttonname =yes9id =answerf1class =lietruth yes >是< / button>< button type =buttonname =no9id =answer1class =lietruth no>否< / button>
< div id =questions> Question2< / div>
< / div>
< / div>
< div id =Answertoggle18>
< div id =questions-box>< input type =hiddenname =question18value =Question3>< input type =hiddenname =keyvalue =25>< input type =hiddenname =uservalue =truthsandlie>< button type =buttonname =yes25id =answerf1class =lietruth yes >是< / button>< button type =buttonname =no25id =answer1class =lietruth no>否< / button>
< div id =questions> Question3< / div>
< / div>
< / div>
< div id =Answertoggle19>
< div id =questions-box>< input type =hiddenname =question19value =Question4>< input type =hiddenname =keyvalue =30>< input type =hiddenname =uservalue =truthsandlie>< button type =buttonname =yes30id =answerf1class =lietruth yes >是< / button>< button type =buttonname =no30id =answer1class =lietruth no>否< / button>
< div id =questions> Question4< / div>
< / div>
< / div>
< div id =Submit-answerdisplay:inline-block;>< button type =submit> Next< / button>
< / div>


解决方案

这是我做的。 (val)和changeValue2(val)。

< b< div id =questions-box>< input type =hiddenname =question18value =Question3>< input type =hiddenname =keyvalue =25 >< input type =hiddenname =uservalue =truthsandlie>< button type =buttonname =yes25onclick =changeValue1('no25')id =answerf1 class =lietruth yes>是< / button>< button type =buttonname =no25onclick =changeValue2('no25')id =answer1class =lietruth no> No< / button>
< input type =hiddenname =no25id =no25value =none>
< div id =questions> Question3< / div>
< / div>
< / div& gt;



< script type =text / javascript>
<! -
function changeValue1(val){
var b = val;
document.getElementById(b).value =yes;
}

function changeValue2(val){
var b = val;
document.getElementById(b).value =no;
}
// - >
< / script>


I have a form with multiple buttons and values. I want to submit which button was clicked when I press the submit button along with the value. I have the code below in a php loop with the questions. The code displays yes and no buttons for each question. I am using jquery to keep the classes active and toggle them (this works fine). My problem is when I submit the form only the questions is submitted via POST, and no data from the buttons pressed. How should I approach this to get the buttons pressed submitted (all buttons have a unique id)?

echo' <button type="button" name="yes'.$value[Key].'" id="answerf1" class="lietruth yes" >Yes</button>

 <button type="button" name="no'.$value[Key].'" id="answer1" class="lietruth no" >No</button>

<div id="questions">'.$value[Question].'</div>';
//submit button is after the loop

HTML of generated page

<div id="Answertoggle16">
<div id="questions-box"><input type="hidden" name="question16" value="Question1"><input type="hidden" name="key"  value="5"><input type="hidden" name="user"  value="truthsandlie"><button type="button" name="yes5" id="answerf1" class="lietruth yes" >Yes</button><button type="button" name="no5" id="answer1" class="lietruth no" >No</button>
<div id="questions">Question1</div>
</div>
</div>
<div id="Answertoggle17">
<div id="questions-box"><input type="hidden" name="question17" value="Question2"><input type="hidden" name="key"  value="9"><input type="hidden" name="user"  value="truthsandlie"><button type="button" name="yes9" id="answerf1" class="lietruth yes" >Yes</button><button type="button" name="no9" id="answer1" class="lietruth no" >No</button>
<div id="questions">Question2</div>
</div>
</div>
<div id="Answertoggle18">
<div id="questions-box"><input type="hidden" name="question18" value="Question3"><input type="hidden" name="key"  value="25"><input type="hidden" name="user"  value="truthsandlie"><button type="button" name="yes25" id="answerf1" class="lietruth yes" >Yes</button><button type="button" name="no25" id="answer1" class="lietruth no" >No</button>
<div id="questions">Question3</div>
</div>
</div>
<div id="Answertoggle19">
<div id="questions-box"><input type="hidden" name="question19" value="Question4"><input type="hidden" name="key"  value="30"><input type="hidden" name="user"  value="truthsandlie"><button type="button" name="yes30" id="answerf1" class="lietruth yes" >Yes</button><button type="button" name="no30" id="answer1" class="lietruth no" >No</button>
<div id="questions">Question4</div>
</div>
</div>
<div id="Submit-answer" display: inline-block;"><button type="submit">Next</button>
</div>

解决方案

Here is what I did. Added a hidden div and change that value using javascript functions changeValue1(val) and changeValue2(val).

<div id="Answertoggle18">
<div id="questions-box"><input type="hidden" name="question18" value="Question3"><input type="hidden" name="key"  value="25"><input type="hidden" name="user"  value="truthsandlie"><button type="button" name="yes25" onclick="changeValue1('no25')" id="answerf1" class="lietruth yes" >Yes</button><button type="button" name="no25" onclick="changeValue2('no25')" id="answer1" class="lietruth no" >No</button>
<input type="hidden" name="no25" id="no25" value="none">
<div id="questions">Question3</div>
</div>
</div>



<script type="text/javascript">
<!--
function changeValue1(val) {
var b = val;
    document.getElementById(b).value = "yes";
}

function changeValue2(val) {
var b = val;
    document.getElementById(b).value = "no";
}
//-->
</script>

这篇关于通过表单发送多个按钮信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆