单选按钮输入在引导程序

时间:2016-12-12 09:43:23

标签: php html twitter-bootstrap

我正在提交一个包含单选按钮输入的表单。如果我从单选按钮输入中删除'class'属性(或没有bootstrap模板显示),它可以工作,但不是。

如果您click any star here并提交,则评分为空。用于生成HTML表单的相应PHP代码(两个链接中相同): test_ratings_curl.php

 <? php
 print('       <form method="post" action="rating_curl.php">
              <span class="rating">');
 $rating = 4;
 $inp_no = 1;
 print('<input type="hidden" name="Name" value="arya" />');
 for ($k = 1; $k <= $rating; $k++) {
   $inp_no = $k;
   $to_print = '<input type="radio" class="rating-input" id="rating_input_'.strval($k).
   '" name="rating_input_'.strval($k).
   '" value="rating_input_'.strval($k).
   '" >
                <label for="rating_input_1_'.strval($k).
   '" class="rating-star1"></label>';
   print($to_print);
 }
 for ($j = $inp_no + 1; $j <= 5; $j++) {
   $to_print = '<input type="radio" class="rating-input" id="rating_input_'.strval($j).
   '" name="rating_input_'.strval($j).
   'value="rating_input_'.strval($k).
   '" >
                <label for="rating_input_1_'.strval($j).
   '" class="rating-star"></label>';
   print($to_print);

 }

 print('  </form>
                &nbsp;');
 print('    
        <input type="submit" name="rating_submit">
        </span>'); ?>

但是会返回here评分。

(两者都有相同的HTML):

<form method="post" action="rating_curl.php">
  <span class="rating">
              <input type="hidden" name="Name" value="arya">

              <input type="radio" class="rating-input" id="rating_input_1" name="rating_input_1" onclick="check(this.id)" value="rating_input_1">
                <label for="rating_input_1_1" class="rating-star1"></label><input type="radio" class="rating-input" id="rating_input_2" name="rating_input_2" onclick="check(this.id)" value="rating_input_2">
                <label for="rating_input_1_2" class="rating-star1"></label><input type="radio" class="rating-input" id="rating_input_3" name="rating_input_3" onclick="check(this.id)" value="rating_input_3">
                <label for="rating_input_1_3" class="rating-star"></label><input type="radio" class="rating-input" id="rating_input_4" name="rating_input_4" onclick="check(this.id)" value="rating_input_3">
                <label for="rating_input_1_4" class="rating-star"></label><input type="radio" class="rating-input" id="rating_input_5" name="rating_input_5" onclick="check(this.id)" value="rating_input_3">
                <label for="rating_input_1_5" class="rating-star"></label>
          <input type="hidden" id="answer" name="answer">
          
                &nbsp;       (43)         
        <input type="submit" name="rating_submit">
        </span>
</form>

提取POST变量的代码(rating_curl.php):

<? php
extract($_POST);

$rating = null;
if (isset($rating_input_1)) {
  $rating = "1";
}
elseif(isset($rating_input_2)) {
  $rating = "2";
}
elseif(isset($rating_input_3)) {
  $rating = "3";
}
elseif(isset($rating_input_4)) {
  $rating = "4";
}
elseif(isset($rating_input_5)) {
  $rating = "5";
}
print('Inside rating_curl.php<br/>');
print("rating= ".$rating);
print('<br/>');
print('hidden: '.strval($answer));
print('<br/>');
print("name of product = ".$Name);
$ch = curl_init();
$post = 'name='.$Name.
'&rating='.$rating;
curl_setopt($ch, CURLOPT_URL, 'gurnoors.com/rating.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);

print('<br/>Curl Results: <br/>');
print($contents);


?>

结论: 如接受的答案所述,label的属性for的值必须与id的{​​{1}}相同。谢谢。

2 个答案:

答案 0 :(得分:1)

如果你想要单选按钮(只有一个选择),你需要添加相同名称的每个输入。

<input type="radio" class="rating-input" id="rating_input_1" name="rating_input" onclick="check(this.id)" value="rating_input_1">

<input type="radio" class="rating-input" id="rating_input_2" name="rating_input" onclick="check(this.id)" value="rating_input_2">

<input type="radio" class="rating-input" id="rating_input_3" name="rating_input" onclick="check(this.id)" value="rating_input_3">

答案 1 :(得分:1)

以下代码有效:

<form method="post" action="rating_curl.php">
              <span class="rating">
              <input type="hidden" name="Name" value="arya">
                <input type="radio" value="rating_input_1" class="rating-input" id="rating_input_1" name="rating_input_1">
                  <label for="rating_input_1_1" class="rating-star1"></label>
                <input type="radio" value="rating_input_2" class="rating-input" id="rating_input_1_2" name="rating_input_2">
                  <label for="rating_input_1_2" class="rating-star1"></label>
                <input type="radio" value="rating_input_3" class="rating-input" id="rating_input_1_3" name="rating_input_3">
                  <label for="rating_input_1_3" class="rating-star"></label>
                <input type="radio" value="rating_input_4" class="rating-input" id="rating_input_1_4" name="rating_input_4">
                  <label for="rating_input_1_4" class="rating-star"></label>
                <input type="radio" value="rating_input_5" class="rating-input" id="rating_input_1_5" name="rating_input_5">
                  <label for="rating_input_1_5" class="rating-star"></label>
          &nbsp;
        <input type="submit" name="rating_submit">
        </span></form>

您需要检查页面上如何生成输入的属性。

现在看起来很奇怪:

<input type="radio" class="rating-input" id="rating_input_1_1" name="rating_input_" 1"="" onclick="check(this.id)" value="rating_input_1">

属性name="rating_input_" 1"=""看起来很奇怪。

label的属性for的值也必须与id的{​​{1}}相同。