douju6542 2016-10-17 16:09
浏览 88
已采纳

有问题保留多个复选框值php [关闭]

Am having trouble retaining the value for my multi checked box array. Have look on this site and have applied the answer to mine, but for some reason is not keeping the checked value. ** here is my PHP **

       <?php

    // start session
    session_start();

    // i user is not log in redirect back to index
    if(!isset($_SESSION['user_type'])){
      header('Location: index.php');
    }

    // include data based connection config
    include_once('include/connection.php');

    // incluude header ttile page
    $title  = "Edit User Profile";

    // set user session variables
    $userId = $_SESSION['user_id'];
    //$userName = $_SESSION['user_name'];
    $temp = $_SESSION['user_name'];

    //$competency = "";
    // if user update the data
      if(isset($_POST['update'])){

       // define post variable
    $firstname = trim($_POST['firstname']);
    $lastname = trim($_POST['lastname']);
    $email = trim($_POST['email']);
    $user_name = trim($_POST['user_name']);
    $profession = trim($_POST['profession']);
    $phone = trim($_POST['phone']);
    $hobbies = trim($_POST['hobbies']);
    $skills = trim($_POST['skills']);
    $competency = implode(', ', $_POST['competency']);
    $postcode = trim($_POST['postcode']);
    $address = trim($_POST['address']);
    $bio = trim($_POST['bio']);  
    $dob = trim($_POST['dob']);
    $gender = trim($_POST['gender']);
    $country = trim($_POST['country']);


          // ************* UPDATE PROFILE INFORMATION ************************//
          if(!($stmt = $con->prepare("UPDATE user SET firstname = ?, lastname = ?, skills = ?, competency = ?, profession = ?, 
          user_name = ?, phone = ?, address = ?, email = ?, bio = ?,
          gender = ?, dob = ?, country = ? WHERE id = ?"))) {
              echo "Prepare failed: (" . $con->errno . ")" . $con->error;
          }
          if(!$stmt->bind_param('ssssssissssssi', $firstname, $lastname, $skills, $competency, $profession, 
                                $user_name, $phone, $address, $email, $bio, 
                                $gender, $dob, $country, $userId)){
            echo "Binding paramaters failed:(" . $stmt->errno . ")" . $stmt->error;
                  }
          if(!$stmt->execute()){
                   echo "Execute failed: (" . $stmt->errno .")" . $stmt->error;
                  }


                  if($stmt) {
                      $_SESSION['main_notice'] = "Successfully Updated!";
                                   header('Location: profile.php');
                                      exit;

                  }else{
                          $_SESSION['main_notice'] = "Some error, try again";
                          header('Location: '.$_SERVER['PHP_SELF']);
                      }

           $stmt->close();



      }

                $stmt = $con->prepare("SELECT firstname, lastname, skills, competency, user_name, avatar, profession, email, dob, gender, country, phone, bio, address, created_at FROM user WHERE id = ?");
            $stmt->bind_param('s', $userId);
            $stmt->execute();
            $stmt->store_result();  
            if($stmt->num_rows == 0) {  
                echo 'No Data Found for this user';
            }else {
                $stmt->bind_result($firstname, $lastname, $skills, $competency, $user_name, $avatar, $profession, $email, $dob, $gender, $country, $phone, $bio, $address, $created_at);
                $stmt->fetch();

              $stmt->close();

            }
                ?>

* And here is my HTML *

       <div class="user-details">

      <form name="update" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" id="UploadForm">

    <table>
    <label for="">Avatar</label>
    <input name="ImageFile" type="file" id="uploadFile"/>
    <!--                     <div>
      <div class="shortpreview">
          <label for="">Previous Avatar </label>
          <br> 
          <img src="userfiles/avatars/<?php //echo $avatar;?>" width='150' height='150' >
      </div>
    </div> -->
    <div class="col-md-6">
      <div class="shortpreview" id="uploadImagePreview">
          <label for="">Current Uploaded Avatar </label>
          <br> 
          <div id="imagePreview"></div>
      </div>
    </div>
    </table>


        <table>
        <tr>
          <td></td>
          <td></td>
          <td>Update</td>
        </tr>
        <tr>
        <td><label for = "">First Name</label></td>
        <td><input type="text" id="firstname" name="firstname" value="<?php echo $firstname; ?>"></td>
        </tr>

        <tr>
        <td><label for = "">Last Name</label></td>
        <td><input type="text" id="lastname" name="lastname"  value ="<?php echo $lastname; ?>"></td>
        </tr>

       <tr>
        <td><label for = "">Skills</label></td>
        <td><input type="text" id="skills" name="skills"  value ="<?php echo $skills; ?>"></td>
        </tr>
    <tr>
    <td>        
    <label >Competency:</label>
    <input type="checkbox" value="RRT" name="competency[]" <?php if(isset($_POST['competency']) && in_array('RRT', $_POST['competency'])) echo 'checked="checked"'; ?>>RRT<br>
    <input type="checkbox" value="ESD" name="competency[]" <?php if(isset($_POST['competency']) && in_array('ESD', $_POST['competency'])) echo 'checked="checked"'; ?>>ESD<br>
    <input type="checkbox" value="SDT" name="competency[]" <?php if(isset($_POST['competency']) && in_array('SDT', $_POST['competency'])) echo 'checked="checked"'; ?>>SDT<br>
    <input type="checkbox" value="CAR" name="competency[]" <?php if(isset($_POST['competency']) && in_array('CAR', $_POST['competency'])) echo 'checked="checked"'; ?>>CAR <br>
    <input type="checkbox" value="DH" name="competency[]" <?php if(isset($_POST['competency']) && in_array('DH', $_POST['competency'])) echo 'checked="checked"'; ?>>DH<br>
    <input type="checkbox" value="MEDS" name="competency[]" <?php if(isset($_POST['competency']) && in_array('MEDS', $_POST['competency'])) echo 'checked="checked"'; ?>>MEDS <br>

    <!--                    <input type="checkbox"  value="RRT" name="competency['RRT']" <?php// if(!empty($_POST['competency']['RRT'])) echo "checked = 'checked'"; ?>>RRT<br>
    <input type="checkbox"  value="ESD" name="competency['ESD']" <?php //if(!empty($_POST['competency']['ESD'])) echo "checked = 'checked'"; ?>>ESD<br>
    <input type="checkbox"  value="SDT" name="competency['SDT']" <?php //if(!empty($_POST['competency']['SDT'])) echo "checked = 'checked'"; ?>>SDT<br>
    <input type="checkbox"  value="CAR" name="competency['CAR']" <?php //if(!empty($_POST['competency']['CAR'])) echo "checked = 'checked'"; ?>>CAR<br>
    <input type="checkbox" value="MEDS"  name="competency['MEDS']" <?php// if(!empty($_POST['competency']['MEDS'])) echo "checked = 'checked'"; ?>>MEDS<br>
    <input type="checkbox" value="DH"  name="competency['DH']" <?php //if(!empty($_POST['competency']['DH'])) echo "checked = 'checked'"; ?>>DH<br> -->
    </td>
    </tr>
    <!--         <tr>        
    <td> 

    <select name="competency[]" multiple="multiple">
    <option value="SDT">SDT</option>
    <option value="ESD">ESD</option>
    <option value="CAR">CAR</option>
    <option value="MEDS">MEDS</option>
    <option value="DH">DH</option>
    </select>

    </td>
    </tr> -->

        <tr>
        <td><label for = "">User Nmae</label></td>
        <td><input type="text" id="user_name" name="user_name" value="<?php echo $user_name; ?>"></td>
        </tr>

        <tr>
        <td><label for = "">Profession Name</label></td>
        <td><input type="text" id="profession" name="profession" value="<?php echo $profession; ?>"></td>
        </tr>                            

        <tr>
        <td><label for = "">Phone</label></td>
        <td><input type="text" id="phone" name="phone" value="<?php echo $phone; ?>"></td>
        </tr>                           

         <tr>
        <td><label for = "">Email</label></td>
        <td><input type="text" id="email" name="email" value="<?php echo $email; ?>"></td>
        </tr>

        <tr>
        <td><label for = "">Gender</label></td>
        <td><input type="text" id="gender" name="gender" value="<?php echo $gender; ?>"></td>
        </tr>

        <tr>
        <td><label for = "">Date Of Birth</label></td>
        <td><input type="text" id="dob" name="dob" value="<?php echo $dob; ?>"></td>
        </tr>


        <tr>
        <td><label for = "">Addres</label></td>
        <td><input type="text" id="address" name="address" value="<?php echo $address; ?>"></td>
          </tr>


        <tr>
        <td><label for = "">Country</label></td>
        <td><input type="text" id="country" name="country" value="<?php echo $country; ?>"></td>
        </tr>  


        <tr>
        <td><label for = "">Bio</label></td>
        <td><input type="text" id="bio" name="bio" value="<?php echo $bio; ?>"></td>
        </tr>

        <tr>
        <td></td>               
        <td><input type="submit" id="update" name="update" value="Update"></td>
        </tr>
        </table>
      </form>       
    </div>

Here is my updated code hopefully there is something causing it.

  • 写回答

1条回答 默认 最新

  • dongmangsha7354 2016-10-17 16:27
    关注

    Try this (updated) :

    <?php $competency = explode(', ', $competency); ?>
    <tr>
        <td>        
                <label >Competency:</label>
                <input type="checkbox" value="RRT" name="competency[]" <?php if(is_array($competency) && in_array('RRT', $competency)) echo 'checked="checked"'; ?>>RRT<br>
                <input type="checkbox" value="ESD" name="competency[]" <?php if(is_array($competency) && in_array('ESD', $competency)) echo 'checked="checked"'; ?>>ESD<br>
                <input type="checkbox" value="SDT" name="competency[]" <?php if(is_array($competency) && in_array('SDT', $competency)) echo 'checked="checked"'; ?>>SDT<br>
                <input type="checkbox" value="CAR" name="competency[]" <?php if(is_array($competency) && in_array('CAR', $competency)) echo 'checked="checked"'; ?>>CAR <br>
                <input type="checkbox" value="DH" name="competency[]" <?php if(is_array($competency) && in_array('DH', $competency)) echo 'checked="checked"'; ?>>DH<br>
                <input type="checkbox" value="MEDS" name="competency[]" <?php if(is_array($competency) && in_array('MEDS', $competency)) echo 'checked="checked"'; ?>>MEDS <br>
         </td>
    </tr>
    

    Also make sur that your form method is post:

    <form methos="POST" ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python动态规划:N根火柴摆出的最大数字
  • ¥20 (标签-excel)
  • ¥200 求idea10和MyEclipse7.1
  • ¥20 vb6.0截取当前窗体保存为jpg文件
  • ¥20 苹果手机不使用大疆sdk怎么获取遥控器控制信息或如何接入大疆sdk并且成功上架sdk
  • ¥20 woocommerce 注册按键重定向
  • ¥100 求书法图像文字切割代码
  • ¥15 同一个波形探测距离不同的目标,为什么fft之后得到的频谱图会发生移动,不应该时移不改变幅度谱吗(标签-matlab)(相关搜索:matlab仿真)
  • ¥15 Proteus仿真程序只能执行一次
  • ¥15 语音识别websocket报错