2015-09-12 135 views
1

我是PHP新手,尝试获取从选择标记中选择的选项的值。我熟悉一些输入类型,如广播,文本,复选框e.t.c.我的问题是“type”是从选择标记中选择的选项。前三种形式工作得很好,但不是第四 这里是我的代码什么“类型”是从选择标记中选择的选项

<form action = display.php method="get"> 
input first name: 
<input type=text name=fname><br> 
input last name: 
<input type=text name=lname><br> 
<input type=submit value="Click"> 
</form> 

<form name="input" 
action=radio.php method="get"> 
Username:<input type="text" name="user"> 
<br> 
<input type=radio name=card_type value="visa "> 
Visa <br> 
<input type=radio name=card_type value="mc"> 
Master Card <br> 
<input type="radio" name=card_type value="discover"> 
Discover <br> 
<input type="submit" value="Submit"/> 
</form> 

<form name="gender" 
action=gender.php method="get"> 
<input type=radio name=gender_type value="male"> 
Male <br> 
<input type=radio name=gender_type value="female"> 
Female <br> 
<input type="submit" value="Submit"/> 
</form> 

<form name="chosen_party" action=party.php method="get"> 
<select> 
<option type=text name=party_type value="democrat">Democrat</option> 
<option type=text name=party_type value="republican">Republican</option> 
<option type=text name=party_type value="independent">Independent</option> 
<option type=text name=party_type value="undecided">Undecided</option> 
</select> 
<input type=submit value="Submit"> 
</form> 

和第四形式的PHP文件...

<?php 
$party_select = $_GET['party_type']; 
echo "User has chosen $party_select"; 
?>  
+0

另外给出答案,''

回答

1

首先给出选择标签的名称。并在选择权的情况下给每个选项赋予一个值。像这样..

<form name="chosen_party" action=party.php method="get"> 
<select name="party_type"> 
<option value="democrat">Democrat</option> 
<option value="republican">Republican</option> 
<option value="independent">Independent</option> 
<option value="undecided">Undecided</option> 
</select> 
<input type=submit value="Submit"> 
</form> 

然后在php中有一个小小的语法问题。您必须将$ party_select放在引号之外。该代码应该是..

<?php 
$party_select = $_GET['party_type']; 
echo 'User has chosen '.$party_select; 
?> 

希望这有助于..

+0

谢谢@Shawon工作 – D0uble0

+0

欢迎..... :) –

+0

'

0

选项说,这是一个选项。根本不需要type属性。

<select name="myOptionList"> 
    <option value="1">First option</option> 
    <option value="2">Second option</option> 
</select> 

这就够了标记,更在这里http://www.w3schools.com/tags/tag_select.asp

1

你需要给name属性的选择,而不是选择。

<form name="chosen_party" action="party.php" method="get"> 
    <select name="party_type"> 
     <option value="democrat">Democrat</option> 
     <option value="republican">Republican</option> 
     <option value="independent">Independent</option> 
     <option value="undecided">Undecided</option> 
    </select> 
    <input type="submit" value="Submit"> 
</form> 

然后在PHP中,你可以访问$_GET['party_type'],这将是选择该选项。

+0

感谢您的帮助 – D0uble0

+0

没问题,很高兴帮助:) – xcvd