简体   繁体   English

获取当前选定单选按钮的隐藏值

[英]get hidden value of the currently selected radio button

i have a form with Variables coming from mysql query , and i passing from a form 2 value of radio and hidden input , no problem with radio .. but hidden not passing Correct value , passing only first value found it on the page . 我有一个带有来自mysql查询的变量的表单,并且我从radio和隐藏输入的表单2值传递,radio ..没问题,但是隐藏不传递正确值,仅传递第一个值在页面上找到它。 I want to get 2 value of radio and hidden together when i Choose the currently selected radio button 当我选择当前选择的单选按钮时,我想获得2个单选值并隐藏在一起

<input type="radio" name="radio" value="<?php echo $awardid ; ?>" />
<input type="hidden" name="point" value="<? echo $point ; ?>" />

after print : 打印后:

<input type="radio" name="radio" value="1">
<input type="hidden" name="point" value="3">

<input type="radio" name="radio" value="2">
<input type="hidden" name="point" value="5">

<input type="radio" name="radio" value="3">
<input type="hidden" name="point" value="8">

elc ...

As an example , when i Choose Second radio , passing Correct value of radio 2 and passing value of hidden 3 -> " that's not Correct value , must be 5 " . 例如,当我选择第二个收音机时,传递收音机2的正确值和隐藏3->“非正确值”的传递值必须为5”。 every and any Choose for radio , passing with it first value of hidden on page -> 3 , when i Change hidden input to radio input and Choose it , passing the Correct value without any problem ... so this problem happen when the input is hidden .. why ? 每个选择收音机,在第-> 3页上将隐藏的第一个值传递给它,当我将隐藏输入更改为收音机输入并选择它时,传递正确的值没有任何问题... ...隐藏..为什么? and Solution ? 和解决方案?

Radio inputs share a name in order to define the group. 单选输入共享一个名称以定义组。

Hidden inputs cannot share names, as they are discreet entities. 隐藏的输入不能共享名称,因为它们是谨慎的实体。

I'd suggest appending the $awardid to the hidden input's name. 我建议在隐藏输入的名称后附加$awardid

<input type="hidden" name="point<?php echo $awardid; ?>" value="<?php echo $point ; ?>" />

Then, you can get the value of that particular input based on the selected radio button. 然后,您可以基于选定的单选按钮获取该特定输入的值。

$radio = $_POST['radio'];
$point = $_POST['point'.$radio];

All of your form elements of the same input type share the same name. 所有具有相同输入类型的表单元素都共享相同的名称。 The name field of the form elements are the keys of the key/value pairs in form submissions. 表单元素的name字段是表单提交中键/值对的键。 Thus, when you have 3 hidden input fields with the same value for the name field, it must choose one. 因此,当您有3个隐藏的输入字段,且name字段的值相同时,它必须选择一个。 Remember, the hidden input fields do not correlate at all with the radio input fields which are simply organized close to each other in the code. 请记住,隐藏的输入字段与无线电输入字段根本不相关,后者在代码中只是彼此紧密地组织在一起。 Your browser does not know that your point fields have anything to do with the radio fields. 您的浏览器不知道您的点字段与无线电字段有任何关系。

You should have unique names for all your input fields, like so 您应该为所有输入字段输入唯一的名称,例如

<input type="radio" name="radio" value="1">
<input type="hidden" name="point1" value="3">

<input type="radio" name="radio" value="2">
<input type="hidden" name="point2" value="5">

<input type="radio" name="radio" value="3">
<input type="hidden" name="point3" value="8">

And then you can retreive their values with 然后,您可以使用

$_POST['radio']
$_POST['point1']

and so on. 等等。

EDIT: However, that does mean that for every form submission, EVERY hidden field's data will be sent every time, regardless of which radio input is selected. 编辑:但是,这的确意味着对于每个表单提交,无论选择哪个无线电输入,每次都会发送每个隐藏字段的数据。 To solve this, you can intercept the form submission on the front end with an event listener such as .submit() , and then disable the input fields you do not want to be submitted prior to allowing the form submission to go through. 要解决此问题,您可以使用.submit()之类的事件侦听器在前端拦截表单提交,然后在允许表单提交通过之前禁用您不想提交的输入字段。

you can rename point to point[radio buttons value] 您可以point重命名point[radio buttons value]

so that when you request point as array, you can get the value from it something like this @$_REQUEST["point"][@$_REQUEST["radio"]]; 这样,当您将点请求为数组时,您可以从中获取类似@$_REQUEST["point"][@$_REQUEST["radio"]];

So as discussed on the comments. 因此,如对评论所述。 I will show my approach. 我将展示我的方法。

So as you mentioned that you have a mysql query to create your inputs (point and radio) I think that is something like: 因此,正如您提到的那样,您有一个mysql查询来创建输入(点和单选),我认为这类似于:

Please disconsider syntaxe as it been a while since I programmed on php 自从我在php上编程以来,已经有一段时间了,请不要使用语法

$query = "select radio, point from someTable where someconditions_here ";
while( /*there is some row*/ ){ 
    echo "<input type=\"radio\" name=\"radio\" value=\"".$row[radio]."\">";
    echo "<input type=\"hidden\" name=\"point\" value=\"".$row[point]."\">";
}

My suggestion would be you just print the radios and after submit it you check the point associated to it, something like this: 我的建议是,您只需打印收音机,然后提交收音机,然后检查与之关联的点,就像这样:

if ( isset($_POST['radio']) ){
    $qryToFindPoint = "select point from someTable where sameconditions_here AND radio = " . $_POST['radio'] ;   
    //do whatever you need here with the selected point
}else{    
   $query = "select radio, point from someTable where someconditions_here ";
   while( /*there is some row*/ ){ 
       echo "<input type=\"radio\" name=\"radio\" value=\"".$row[radio]."\">";
   }
}

This is just the idea. 这只是想法。 Of course you should use proper mysqli or PDO functions to do your queries and avoid sql injection. 当然,您应该使用适当的mysqliPDO函数进行查询并避免sql注入。

With this approach you would avoid also a HTML injection. 使用这种方法,您还可以避免HTML注入。 Say that in your final result as another answer suggests has this: 说您的最终结果是另一个答案,它暗示了这一点:

<input type="hidden" name="point1" value="4" />

Anyone can edit the html code and change this value to lets say: 任何人都可以编辑html代码并将此值更改为可以这样说:

<input type="hidden" name="point1" value="400000" />

And then submit it. 然后提交。 As you would not check, the value of the point it would not be the right one. 正如您不会检查的那样,该点的值将不是正确的值。

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

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