2014-09-24 76 views
0

我有一個PHP文件中的類的數組,這個類有3個字段,我使用這些字段來保存SELECTS和INPUT CHECKBOX在FORM內部使用POST,然後我需要將這個類的數組發送到另一個用於處理數據的php文件。如何發送一個PHP數組類的值從一個PHP文件到另一個PHP文件

<?php 
    class info_subject{ 

    public $code_as; 
    public $time_as; 
    public $selectionn_as; 

     } 

    $subjects[0] = new info_subject();//The only way I have seen for creating an array of classes 
    //is with a for loop, but if you have a better way for doing this, please let me know  
    $subjects[1] = new info_subject(); 

    //here i am using the fields to save info in a form 

$i=0; 
echo "<form name = 'formsubjects' method='post' action='file2.php'>"; 
echo "<select name=\"".$subjects[$i]->time_as ."\">"; 
//options 
echo "</select>"; 
echo "<input type='checkbox' name=\"".$asignaturas[$i]->selection_as."\">"; 
//and so on with every position of the array using $i 
?> 

//then there is a button to send the the dato to file2.php 

//file2.php 
<?php 

$subjects=$_POST["$subjects"];//I am using this but i cant retrieve the fields of $subjects 

    ?> 

我該怎麼辦好友?

+2

你絕對應該看看MVC架構。 – 2014-09-24 01:08:24

+0

什麼是好友?... – 2014-09-24 01:10:51

+0

一種編程方式,不會讓你像HTML那樣混合使用HTML,也就是說,它不能解決你的問題。對於你的問題,我建議你創建一個方法返回你需要的變量,然後在你的其他文件中使用你的類,並調用你剛剛實現的函數 – 2014-09-24 01:21:47

回答

0

$i=0; 
echo "<form name = 'formsubjects' method='post' action='file2.php'>"; 
echo "<select name=\"".$subjects[$i]->time_as ."\">"; 
//options 
echo "</select>"; 
echo "<input type='checkbox' name=\"".$asignaturas[$i]->selection_as."\">"; 

你可以嘗試這樣的:

$i=0; 
echo "<form name = 'formsubjects' method='post' action='file2.php'>"; 
echo "<select name=\"subjects[$i][".$subjects[$i]->time_as ."]\">"; 
//options 
echo "</select>"; 
echo "<input type='checkbox' name=\"subjects[$i][".$asignaturas[$i]->selection_as."]\">"; 

,然後在文件2

$subjects=$_POST["subjects"]; 

,並摘下你需要什麼。

說實話,我覺得你的問題很難理解。也許嘗試簡化你的問題,並清理一些代碼/格式,我們會更容易給你建議。

0

您不能通過$ _POST提交一個類或數組,因爲post數組是一個只能包含簡單值的數組。

+0

好的,我能做些什麼來解決這個問題? – 2014-09-24 01:43:22

0

嗨〜Jose Ricardo Citerio。我希望你能讀中國

我不知道你是不是需要這樣,在文件1裏面存儲着選擇的選項選項。然後文件2接收去後處理

<?php 

類info_subject {

public $code_as = ['name' => 'select_name', 'option' => ["k" => "v"]]; 
public $time_as; 
public $selectionn_as; 

}

$ subjects = new info_subject();

echo「」; echo'code_as [「name」]。 ' 「>」'; //循環選項$ subject [「選項」]並打印k和v; like => foreach($ subjects-> code_as [「name」]爲$ k => $ v){echo''。 $ v。 '';} echo「」; echo'xxx ['name']。 '「>請檢查';

>

//然後有一個按鈕發送的拿督到file2.php

//file2.php

<?php 
$subjects = $_POST["select_name"]; 
$subjects1 = $_POST["checkbox_name"]; 

?代替>