2011-10-11 63 views
0

我無法弄清楚,並嘗試了幾種方法。如何在使用對象時回顯php會話

我有一個多頁的表單,其中form_1.php,提交後,發送信息到數據庫並重定向到form_2.php。

我上form_1.php啓動一個會話使用:

<?PHP 
if ($_POST) 
{ 
    session_start(); 

    foreach ($_POST as $field => $value) 
    { 
     $_SESSION['formdata'][$field] = $value; 
    } 
} 
.....code that sends info to db and then redirects page to form_2.php 
?> 

當我到form_2.php我:

<?php 

session_start(); 

if (!empty($_POST)) { 

    // Used for later to determine result 
    $success = $error = false; 

    // Object syntax looks better and is easier to use than arrays to me 
    $post = new stdClass; 

    // Usually there would be much more validation and filtering, but this 
    // will work for now. 
    foreach ($_POST as $key => $val) 
     $post->$key = trim(strip_tags($_POST[$key])); 

....more code that loads dompdf and sends a pdf to email 
?> 

如果我使用類似:

<?php echo $post->Name; ?> 
<?php echo $post->Address; ?> 
<?php echo $post->City; ?> 
<?php echo $post->State; ?> 

我想要顯示的數據的地方我什麼也沒得到。

有沒有寫這一點,這相當於一個辦法:

<?php echo $_SESSION['Name'] ?> 

難道是因爲我使用的對象?

如果我擦一切從form_2.php,只是打印使用會話:

<?php 
session_start(); 
Print_r ($_SESSION); 
?> 

我看我要像數據:

Array ([sfm_from_iframe] => 0 [formdata] => Array ([sfm_form_submitted] => yes [Employer_Zip] => 33333 [Injury_type] => Array ([0] => Head [1] => Left Shoulder [2] => Chest) [FirstName] => test [LastName] => tester [Address] => 1212 myaddy [City] => city1 [State] => Maine [Zip] => 55555 [Country] => AntiguaAndBarbuda [Phone] => 555-555-5555 [Email] => [email protected] [Employer_Name] => my employer [Employer_Address] => 1212 empaddy [Employer_City] => city2 [Employer_State] => Georgia [DBA_Carrier] => mycarrier [Employer_Phone] => 333-333-3333 [Accident_Date] => 10-03-2011 [Message] => asefsafsadf sadf asdfsdafsadf [yes_no] => yes [SocialSecurity] => 333-33-3333 [Submit_x] => 57 [Submit_y] => 13)) 

其他注意事項,可能會有幫助...

當我只是用POST來form_2.php和使用

<?php echo $post->Name; ?> 
<?php echo $post->Address; ?> 
<?php echo $post->City; ?> 

一切都顯示和工作正常,但只使用POST直接到該頁面。不使用重定向,因爲我認爲我搞砸了如何回顯SESSION對象。

有什麼想法?

+0

我不太明白你會議的麻煩是什麼。我們可以得到form_2.php的源代碼嗎? –

回答

0

你重定向這樣的:

在DB插入

Form1中-submit->文件-redirect->窗口2

如果是這樣的話,那麼你應該知道,POST數據不會被轉發到第三頁。如果沒有更多的信息,很難給出一個好的答案,但也許你應該在Form2的頂部包含數據庫插入腳本,並將form1直接轉發到form2,或者將POST數據保存到會話中,然後在form2的頂部獲取。

希望幫助:)

+0

我使用simfatic表單(軟件)將所有信息發送到數據庫...它只是在提交後創建一個重定向,所以我不知道如果我是正確的或沒有時表示沒有POST form_2.php。只是一個重定向。來自simfatic的代碼只是定義了這樣的重定向:$ tupage = new FM_ThankYouPage(); $ tupage-> SetRedirURL(「http://myurl.com/pdf_email/form_2.php」); $ formmailobj-> addModule($ tupage); – rick

0

你混合由您的OOP框架提供的代碼(可能是某種CMS),並嘗試使用$後,當你的數據是不存在的。

$ _SESSION用於在頁面更改之間保留數據。你可以在你的form2.php使用下面的代碼都合併在一起:

foreach ($_POST as $key => $val){ 
     $post->$key = trim(strip_tags($_POST[$key])); 
} 
foreach($_SESSION['formdata'] as $key => $val){ 
     $post->$key = trim(strip_tags($_POST[$key])); 
} 

然後,當您檢查$ post對象,你現在應該在那裏,你的數據。

祝你好運

+0

這似乎沒有工作......真的失去了這一點。 – rick

+0

我相信你的權利,正在混合的東西... 這是作爲一個黑客simfatic形式添加: if($ _POST) { \t session_start(); \t \t 的foreach($ _POST爲$字段=> $值) \t { \t \t $ _SESSION [ 'FORMDATA'] [$字段] = $值; \t \t}} 這是對正在使用的form_2因爲形式使用DOMPDF I 2,這更容易 //對象語法看起來更好,更容易比陣列使用我 \t $ =後新stdClass的; \t \t //通常會有更多的驗證和過濾,但這個 \t //現在可以使用。 \t foreach($ _POST as $ key => $ val) \t \t $ post - > $ key = trim(strip_tags($ _ POST [$ key])); – rick