2015-11-23 71 views
-1

我在寻找一种方法来从表单创建阵列,并将其转移到另外一个页面: 这是我的实际代码:创建和transfert阵列PHP

<?php 
$nba = $_GET["nbadultes"]; // recup number of passangers 
$adu = array(); 

for ($i=1;$i<=$nba;$i++) 
{ 
    echo '<h3>Passager '.$i.'</h3> 
     <label>CIVILITÉ</label> 
      <select class="full-width" name="Civilite"> 
       <option value="Mr" selected >Mr.</option> 
       <option value="Mrs" >Mrs.</option> 
      </select> 
     <label>FIRSTNAME *</label> 
     <input type="text" name="FIRSTNAME"/> 
     <label>LASTNAME *</label> 
     <input type="text" name="LASTNAME"/> '; 

    $adu[$i][civilite] = $_POST['civilite']; 
    $adu[$i][FIRSTNAME] = $_POST['FIRSTNAME']; 
    $adu[$i][LASTNAME] = $_POST['LASTNAME']; 
} 

$_SESSION['liste_adultes'] =$adu; 
?> 

而这种代码在接下来的页面阅读数组:

 <?php 
     if (isset($_SESSION['liste_adultes'])) 
     print_r ($liste_adultes); 
     ?> 

但我不知道为什么数组为空:

Array 
(
[1] => Array 
    (
     [Civilite] => 
     [FIRSTNAME ] => 
     [LASTNAME] => 
    ) 

[2] => Array 
    (
     [Civilite] => 
     [FIRSTNAME ] => 
     [LASTNAME] => 
    ) 

) 
+1

'的print_r($ _ POST);'请。 –

+1

反问:会话是否开始使用会话的所有页面?如果没有,那么你去。 –

+0

'print_r($ liste_adultes);'你的意思是'print_r($ _SESSION ['liste_adultes']);'?那是什么'$ liste_adultes'? – Federkun

回答

0
  1. 您需要照顾的形式输入您发送
  2. 正如我所看到的,现在的形式没有提交,所以你并不需要一个会议,但一个将操作设置为处理发送数据的页面的表单。喜欢的东西:

    <form method="post" action="my-action-file.php"> 
    <?php 
    $nba = $_GET["nbadultes"]; 
    for ($i = 1; $i <= $nba; $i++) { 
        echo '<h3>Passager '.$i.'</h3> 
         <label>CIVILITÉ</label> 
          <select class="full-width" name="Civilite[]"> 
           <option value="Mr" selected >Mr.</option> 
           <option value="Mrs" >Mrs.</option> 
          </select> 
         <label>FIRSTNAME *</label> 
         <input type="text" name="FIRSTNAME[]"/> 
         <label>LASTNAME *</label> 
         <input type="text" name="LASTNAME[]"/> '; 
    } 
    ?> 
        <input type="submit" value="Submit" /> 
    </form> 
    

my-action-file.php文件,你可以得到$_POST变量:

<?php 
    foreach ($_POST['Civilite'] as $key => $civilite) { 
     $firstName = $_POST['FIRSTNAME'][$key]; 
     $lastName = $_POST['LASTNAME'][$key]; 

     // now you have the sent data.. just use it like you need.. 
    } 

    // of course you can add these data to an array if you like so 
+0

这看起来很有希望。 –

+1

它工作!谢谢 ! – Senneville

0

在你需要读取会话数据的第二页,然后读取保存在session到你所使用的变量的具体值:

<?php 
    if(!isset($_SESSION)){ session_start();} 
    if(isset($_SESSION['liste_adultes'])){ 
     $liste_adultes = $_SESSION['liste_adultes']; 
     print_r($liste_adultes); 
    } 
?> 

此外,在第一页,请确保您发布的数据和阅读与$ _POST;或通过查询字符串$ _GET

+1

你也错过了一些东西。 –

+0

谢谢弗雷德。你是对的。 –

+0

我不得不坐下来,只是指着她,代码中的错误。 –