я хочу, чтобы отобразить значение переключателя от MySQL в PHP - PullRequest
0 голосов
/ 08 января 2019
<div class="form-group">
    <label>Gender</label>
    <label class="radio-inline"><input type="radio"   name="gender" value="male"<?php
        if ($row["gender"] == 'male') {
            echo"checked";
        }
        ?>/>male</label>
    <label class="radio-inline"><input type="radio"  name="gender" value="female" <?php
        if ($row["gender"] == 'female') {
            echo "checked";
        }
        ?>/>Female</label>
    <label class="radio-inline"><input type="radio" value="others" name="gender"
       <?php if ($row["gender"] == 'others') {
             echo "checked";
            }?>/>Others</label>
</div>

я хочу получить значение переключателя из mysql по php, результат не отображается

Ответы [ 3 ]

0 голосов
/ 08 января 2019

Вы почти там, единственная проблема - это пробел между атрибутами "value" и "checked". Ваш код будет переведен на:

<input type="radio" name="gender" value="male"checked/>

Должно быть:

<input type="radio" name="gender" value="male" checked />

Вы можете сделать это проще так:

<div class="form-group">
    <label>Gender</label>
    <label class="radio-inline">
        <input type="radio" name="gender" value="male" <?=$row["gender"] == 'male' ? 'checked' : ''?>/>
        Male
    </label>
    <label class="radio-inline">
        <input type="radio"  name="gender" value="female" <?=$row["gender"] == 'female' ? 'checked' : ''?>/>
        Female
    </label>
    <label class="radio-inline">
        <input type="radio" value="others" name="gender" <?=$row["gender"] == 'others' ? 'checked' : ''?>/>
        Others
    </label>
</div>
0 голосов
/ 09 января 2019
 <h2>CRUD OPERATION</h2>
                <?php
                 $id=$_GET['id'];
                $query_update = "select * from crudtable where id=$id";
                $res_update = mysqli_query($conn, $query_update);
                ?>    
                <!--crud form start here-->
                <form action="" method="post" enctype="multipart/form-data">
                    <?php
                      $row_update = mysqli_fetch_array($res_update);
                      $id = $row_update['id'];
                      $name=$row_update['first_name'];
                      $gender=$row_update['gender'];
                    ?>
                    <div class="form-group">
                        <label for="name">First Name</label>
                        <input type="text" class="form-control" name="fname" value="<?php echo $row_update['first_name']; ?>">
                    </div>
                    <div class="form-group">
                        <label for="lastname">Last name</label>
                        <input type="text" class="form-control" name="lname" value ="<?php echo $row_update['last_name']; ?>">
                    </div>
                    <div class="form-group">
                        <label for="email">Email</label>
                        <input type="email" class="form-control" name="email" value ="<?php echo $row_update['email']; ?>">
                    </div>
                    <div class="form-group">
                        <label>Phone</label>
                        <input type="text" class="form-control" name="phone" value ="<?php echo $row_update['phone']; ?>">
                    </div>
                    <div class="form-group">
                        <label>Gender:</label>
                        <label class="radio-inline">
                            <input type="radio" name="gender"  <?php if($gender == 'male'){
                                echo "checked"; } ?> value= "male"/>
                            Male
                        </label>
                       <label class="radio-inline">
                            <input type="radio" name="gender"  <?php if($gender == 'female') {
                                echo "checked";
                                }?> value= "female"/>
                            Female
                        </label>
                       <label class="radio-inline">
                            <input type="radio" name="gender" value= "others" <?php if($gender == 'others'){
                                echo "checked";
                            }?> />
                            Others
                       </label>

                    </div>
                    <div class="form-group">
                        <label>country</label>
                        <select class="form-control" name="country" >
                            <option>india</option>
                            <option>pakistan</option>
                            <option>australia</option>
                            <option>usa</option>
                            <option>Uk</option>
                            <option>nepal</option>
                            <option>australia</option>
                            <option>usa</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Hobbies</label>
                        <label class="checkbox-inline"><input  type="checkbox"  value="cricket" name="hobbies[]">Cricket</label>
                        <label class="checkbox-inline"><input  type="checkbox" value="singing"  name="hobbies[]">singing</label>
                        <label class="checkbox-inline"><input  type="checkbox"  value="dancing" name="hobbies[]">dancing</label>
                    </div>
                    <div class="form-group">
                        <textarea type="textarea" class="form-control" rows="5" name="message">
                        </textarea>
                    </div>
                    <div class="form-group">
                        <label>Image </label>
                        <input type="file" name="uploadfiles">
                    </div>

                    <div class="form-group">
                        <input tenter code hereype="submit" class="btn btn-default btn-primary form-control " value="submit" name="updatebtn">
                    </div>

                </form>


  [1]: https://i.stack.imgur.com/h5ekf.png
0 голосов
/ 08 января 2019

Это должно работать, и выглядит немного аккуратнее:

<?php
  function echoChecked($val){
    if($row["gender"] == $val){
      return 'checked = "checked"';
    }
  }
?>
<div class="form-group">
  <label>Gender</label>
  <label class="radio-inline">
    <input type="radio" name="gender" value="male" <?= echoChecked("male") ?> />
    Male
  </label>
  <label class="radio-inline">
    <input type="radio" name="gender" value="female" <?= echoChecked("female") ?> />
    Female
  </label>
  <label class="radio-inline">
    <input type="radio" name="gender" value="others" <?= echoChecked("others") ?> />
    Others
  </label>
</div>
...