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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值