2017-05-02 59 views
3
<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> 

一个html单选按钮的定制属性值要知道如何使用PHP来获得像上面几个例子单选按钮之间的定制属性的值。我如何获得在PHP

我如何在php中获得customizedValue1和customizedValue2的值?

感谢

+0

数组如果妳想要然后在PHP这些自定义属性的值,使用JavaScript来获取这些值与使一个AJAX调用相应的PHP文件中使用所有这些数据。 – mi6crazyheart

+0

嗨!我一起使用php和ajax并不是很清楚,请你给我看一些例子吗? –

回答

1

你不能从PHP直接访问这个值,你需要将它们POST值传递的AJAX到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']; 
+0

嗨Troyer,我需要在

标记中添加一些代码来使用脚本吗?我在我的文件中试过这段代码,但它仍然无法工作。顺便说一句,的类型是“无线电”,所以我应该如何改变你输入的代码? –

+0

我添加了表单的HTML,你应该看看AJAX是什么以及它是如何工作的,这里要广泛地解释它。 – Troyer

+0

我使用alert(customizedValue1)验证脚本中单选按钮的值;在var customizedValue1 = $('#test input [name = sample]:checked')。attr('customizedValue1');但不知何故,它不显示在getValue.php。 –

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; 
} 
?> 
+0

谢谢,定制的值没有必要连接。 –