如何将多个ID传递给jobCart页面?

时间:2016-01-23 15:34:29

标签: php html mysql

我有一个作业列表页面,显示所有作业,每个作业旁边都有一个复选框,页面底部有一个添加按钮,将其添加到JobCart.php

老实说,我不知道如何将多个记录ID或单个ID传递给jobCart.php 我希望用户在单击“添加”按钮时将所有选定的ID传递给jobCart.php 请帮帮我

            <?php
        // adding JobsLists.php to this page to interact witht it.
          require ("../JobsLists.php");

        //Connect to DB
        //include_once("Project/CIEconn.php");
        $mysqlCON= mysqli_connect("localhost", "root", "","CIE") or die(mysqli_connect_error()); 
        mysqli_select_db($mysqlCON,'CIE') or die ("no database");

        $ID = isset($_POST['Id']); // 1 2 

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

      if( empty($ID) || $ID == 0 ){
      echo"<h4>   please choose something to move to your job list  </h4>";
     }else{

      // Code here ..
    // here here ONLY for TEST to check if I can interact eith jobLists.php
    addJob(); 

// to get all ID from each selected job
   $impid = implode("' , '" ,  $_POST['Id']);


     }

      }





        $sqlCommand = "SELECT * FROM  Fiscal WHERE NoStudent > '0'  ";
        $result = mysqli_query($mysqlCON,$sqlCommand) or die(mysqli_error($mysqlCON)); 


        echo '
        <form action= "Fiscal.php"  method = "post">
        <table width ="100%" cellpadding ="4" border="1" >

        <tr>
        <th>Check </th>
        <th>Jobs Name</th>
        <th>Description</th>
        <th> No Students needed</th>
        <th>Due Date</th>
        </tr>';

        while ($row = mysqli_fetch_array($result) ){

        // name = 'Id[]'
            echo "<tr>

        <td> <input type='checkbox'  name='Id[]'  value='". $row['Id'] ."' /> </td>


         <td> ". $row['JobName'] ." </td>
         <td> ". $row['Description'] ." </td>
         <td> ". $row['NoStudent'] . "</td>
        <td>". $row['DueDate'] ." </td>
        </tr>";


        }

        echo '
        </table>
        <br/>

        <div align="center">
        <input type="submit" name="pick" value="Add Job"  />

        <input type="reset" value="Clear Marks" />
        </div>

        </form>

        ';

        ?>

        <html>
        <head><title> Fiscal </title></head>
        <br>
        <body>
        </body>
        </html>

1 个答案:

答案 0 :(得分:0)

首先,在页面的最顶部开始会话,如下所示:

<?php
    session_start();
?>

在表单处理期间将$_POST['Id']数组存储到$_SESSION超全局,并使用header()函数将用户重定向到jobCart.php页面。

还有一件事,$_POST['Id']将是一个数组,因此使用count()函数来查看它是否为空。所以你的代码应该是这样的:

// your code

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

    if(count($_POST['Id'])){
        // store `$_POST['Id']` array to `$_SESSION` superglobal
        $_SESSION['ids'] = $_POST['Id'];

        // redirect the user to jobCart.php page
        header("Location: jobCart.php");
        exit();
    }

}

// your code

在jobCart.php中你可以这样做:

$impid = implode("','" , $_SESSION['ids']);

// rest of your code