dongren2128 2017-05-31 04:57
浏览 74

复选框无法正常工作

I'm new to PHP and I hope someone can help me. I have 4 PHP files and they are basically a form for the user to fill, then the system will navigate it to validate page, and user will click submit to save it. I have a problem in the checkbox part.

The error shows :

Notice: Array to string conversion in C:\xampp\htdocs\LM\LMvalidate_reservation.php

I have simplified the code to show only the checkbox part for easier understanding. I hope someone(s) can help me in this.

LMreservation.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="LMvalidate_reservation.php" method="post">
  
      <div class="col-md-4"><b>Please check (√ ) the module(s) that you want to attend:</b><br></div>
        <div class="col-md-8">
        <input type="checkbox" class="get_value" value="WEBOPAC Usage">WEBOPAC Usage<br>
        <input type="checkbox" class="get_value" value="Accessing Online Database Skill">Accessing Online Database Skill<br>
        <input type="checkbox" class="get_value" value="E-Books and E-Journals Accession">E-Books and E-Journals Accession<br>
        <input type="checkbox" class="get_value" value="Digital Collection Accession">Digital Collection Accession<br>
        <input type="checkbox" class="get_value" value="EQPS Exam Papers">EQPS Exam Papers<br>
        <input type="checkbox" class="get_value" value="Information Searching Strategy">Information Searching Strategy<br>
        <input type="checkbox" class="get_value" value="SCOPUS & Web Of Science Usage Skill">SCOPUS & Web Of Science Usage Skill<br>
        <input type="checkbox" class="get_value" value="Reference Management Software (EndNote & Mendeley)">Reference Management Software (EndNote & Mendeley)<br>
        <input type="checkbox" class="get_value" value="UiTM Institutional Repository (Thesis & Dissertation)">UiTM Institutional Repository (Thesis & Dissertation)<br>
        <input type="checkbox" class="get_value" value="Digital Map">Digital Map<br>
        <input type="checkbox" class="get_value" value="E-Newspaper (BLIS (Bernama Library & Infolink Service)">E-Newspaper (BLIS (Bernama Library & Infolink Service))<br>
        <input type="checkbox" class="get_value" value="Facility">Facility<br><br>
        </div>
      


          <script>
            $(document).ready(function(){
            $('#submit').click(function(){
            var insert = [];
            $('.get_value').each(function(){
            if($(this).is(":checked"))
            {
            insert.push($(this).val());
            }
            });
            insert = insert.toString();
            $.ajax({
            url: "insert.php",
            method: "POST",
            data:{insert:insert},
            success:function(data){
            $('#result').html(data);
            }
            });
            });
            });
           </script>
           

<input type="submit" name="LMreservation_form" value="Submit"> 


</form>

LMvalidate_reservation.php

<?php

if (isset($_POST['LMreservation_form'])) {

  $module = "";
  $count_error = 0;
  $msg = "";

  // validate if submitted variables empty show error msg else put in local variables

  }
  if (isset($_POST['module']) && ($_POST['module'] != ""))
    $module = $_POST['module'];
  else
  {
    $msg .= "Error: Please select your module.<br>";
    $count_error++;
  }

  if ($count_error > 0) {
    echo $msg;
    echo "$count_error error(s) detected.";
    die();
    // display error(s) here and stop
  }
}
else {
  echo "Error: You have execute a wrong PHP. Please contact the web administrator.";
  die();
}

?>

<!DOCTYPE html>
<html>
<head>

</head>

<body>

<form action="LMsave_reservation.php" method="post">
<table> 
     <tr>
      <td class="col-md-4 col-xs-4">Module:</td>
      <td class="col-md-8 col-xs-8"><?php print $module; ?><input type="hidden" name="module" value="<?php echo $module; ?>"></td>
    </tr>


  </table><br>

  <input type="submit" name="LMreservation_validate" value="Save My Reservation">


</form>

</body>
</html>

LMsave_reservation.php

<?php

if (isset($_POST['LMreservation_validate'])) {

  // Connection variables
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "lm_reservation";

  try {
      $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      // Prepare the SQL statement
      $stmt = $conn->prepare("INSERT INTO lmreservation(name, studentstaffid, faculty, contactno, email, program, participant, attandance, module, date, starttime, endtime) VALUES (:name, :studentstaffid, :faculty, :contactno, :email, :program, :participant, :attandance, :module, :date, :starttime, :endtime)");

      // Bind the parameters

      $stmt->bindParam(':module', $module, PDO::PARAM_STR);


      // insert a row

      $module = $_POST['module'];

      $stmt->execute();

      echo "Your application is successful. Have a nice day! :)";
      }

    catch(PDOException $e)
    {
        echo "Error: " . $e->getMessage();
    }

    $conn = null;
  }

 ?>

insert.php

<?php
if(isset($_POST["insert"]))
{
 $conn = mysqli_connect("localhost", "root", "", "lm_reservation");
 $query = "INSERT INTO lmreservation(modules) VALUES ('".$_POST["insert"]."')";
 $result = mysqli_query($conn, $query);
}
?>
</div>
  • 写回答

2条回答 默认 最新

  • dshkmamau65777662 2017-05-31 05:17
    关注

    use a For loop instead in that file LMvalidate_reservation.php

    <?php
     
    if (isset($_POST['LMreservation_form'])) {
     
      $module = "";
      $count_error = 0;
      $msg = "";
     
      // validate if submitted variables empty show error msg else put in local variables
     
      }
      if (isset($_POST['module']) && ($_POST['module'] != ""))
        $module = $_POST['module'];
      else
      {
        $msg .= "Error: Please select your module.<br>";
        $count_error++;
      }
      
      if ($count_error > 0) {
        echo $msg;
        echo "$count_error error(s) detected.";
        die();
        // display error(s) here and stop
      }
    }
    else {
      echo "Error: You have execute a wrong PHP. Please contact the web administrator.";
      die();
    }
     
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
      
    </head>
    
    <body>
    
    <form action="LMsave_reservation.php" method="post">
      
    <table> 
      <?php foreach($module as $element) { ?>
         <tr>
          <td class="col-md-4 col-xs-4">Module:</td>
          <td class="col-md-8 col-xs-8"><?php print $element; ?><input type="hidden" name="module" value="<?php echo $element; ?>"></td>
        </tr>
      <?php } ?>
       
      </table><br>
    
      <input type="submit" name="LMreservation_validate" value="Save My Reservation">
    
    
    </form>
    
    </body>
    </html>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。