【问题标题】:How to check a Radio button from the value of a table of mySQL如何从 mySQL 表的值中检查单选按钮
【发布时间】:2014-11-12 00:11:29
【问题描述】:

我有一个包含字段名称和性别的表格。我得到数据并尝试在输入字段中显示信息,它适用于输入文本名称,但我想知道是否可以根据从表中获得的数据检查单选按钮?

<?php 
$sql="select * from empleo where id='$id';"; 

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));}

$cs=mysqli_query($con,$sql);
        while($resul=mysqli_fetch_array($cs)){
            $name=$resul[0];
            $sexo=$resul[1];
            }
?>

Name: <input  type="text" value="<?php echo $name?>"/><br>

<label for="radio">Hombre</label>
<input type="radio" name="gender" id="radio" value="men">

<label for="radio" > Mujer</label>
<input type="radio" name="gender" id="radio" value="women">

【问题讨论】:

    标签: php jquery html mysql get


    【解决方案1】:

    假设您的 $result[1] 是女性的 W 和男性的 M如果不是,请更改代码):

    <label for="radio">Hombre</label>
    <input type="radio" name="gender" id="radio" value="men" <?php if($sexo=="M") echo "checked" ?>>
    
    <label for="radio" > Mujer</label>
    <input type="radio" name="gender" id="radio2" value="women" <?php if($sexo=="W") echo "checked" ?>>
    

    阅读Radio Button Input

    【讨论】:

      【解决方案2】:
      <label for="sexo-men">Hombre</label>
      <input type="radio" name="gender" id="sexo-men" value="men" <?php $sexio === 'men' ? 'checked="checked"' : '' ?> />
      
      <label for="sexo-women">Mujer</label>
      <input type="radio" name="gender" id="sexo-women" value="women" <?php $sexio === 'women' ? 'checked="checked"' : '' ?> />
      

      而且html的id属性在页面上必须是唯一的。

      【讨论】:

      • 是的,感谢您告诉我有关 ID 的信息。我尝试了您的代码,但没有工作:( IDK 为什么我喜欢这个论坛
      【解决方案3】:

      检查 HTML 中的复选框是通过 checked 属性完成的:

      <input name="awesome" type="checkbox" checked="checked" /> 
      

      所以我们可以设置一个小函数来帮助我们输出这个:

      function check_if($condition) {
          if ($condition) {
              echo 'checked="checked"';
          }
      }
      

      您的脚本的问题是您只为empleo 表中的第一条记录设置变量。 您最可能想要的是循环遍历所有记录:

      // Lets get all the records from result and as an associative array
      $employees = mysqli_fetch_all($cs, MYSQLI_ASSOC);
      // this lets us use column names instead of indexes - much easier to read the code.
      foreach($employees as $e) { ?>
      
          <label for="name">Name<label><input name="name" type="text" value="<?php echo $e['name']?>"/>
      
          <label for="gender">Hombre</label>
          <input type="radio" name="gender" id="radio" value="male" <?php check_if($e['gender'] === 'male') ?>>
      
          <label for="gender">Mujer</label>
          <input type="radio" name="gender" id="radio" value="female" <?php check_if($e['gender'] === 'female') ?>>
      
      <?php } // end foreach ?>
      

      我假设empleo 表中的列是gendername

      您还应该注意每个&lt;input&gt; 应该有一个name 属性,每个&lt;label&gt; 应该有一个for 与它标记的输入的name 匹配的属性。

      【讨论】:

      • 在描述一个人的性别时,正确的英文术语是“男性”和“女性”。
      • 谢谢别人给了我答案,所以我不必尝试你的答案,谢谢!再次感谢您!
      猜你喜欢
      • 2020-10-11
      • 2022-07-20
      • 2016-02-08
      • 2011-10-04
      • 1970-01-01
      • 2012-03-08
      • 2017-03-19
      • 2017-09-03
      • 1970-01-01
      相关资源
      最近更新 更多