如何在php中获取html单选按钮的自定义属性值

时间:2017-05-02 09:43:37

标签: php html

<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>

我想知道如何通过使用php获取上面几个单选按钮之间的自定义属性的值。

如何在php中获取customizeValue1和customizedValue2的值?

由于

2 个答案:

答案 0 :(得分:1)

您无法直接从PHP访问此值,您需要将它们作为AJAX POST值传递给PHP文件,如下所示:

<强> FORM

<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>

<强> JS

$('#test').on('submit',function(){

   var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');

   $.post('getValue.php',{'customizedValue1':customizedValue1});

});

getValue.php 上,您可以访问以下值:

echo $_REQUEST['customizedValue1'];

答案 1 :(得分:0)

如果他们以某种方式相互连接。您还可以将值用作html格式的数组

<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
if(isset($_POST['submit'])){
   $customizedValue1 = $_POST['data']['A']['customizedValue1'];
   $customizedValue2 = $_POST['data']['A']['customizedValue2'];
   echo $customizedValue1;
   echo $customizedValue2;
}
?>