2017-10-10 106 views
0

我想在preview.php中顯示$new_client變量,但我沒有得到任何值,如果我將任何其他變量設置爲klmn會話,那麼它工作正常。哪裏不對?

pdf/index.php

<?php 
session_start(); 
if(!isset($_SERVER['HTTP_REFERER'])){ 
    header('location:../error.php'); 
    exit; 
}; 

include("add_client.php"); 
$_SESSION['klnm']=$new_client; 
echo $_SESSION['klnm']; 

pdf/add_client.php

<?php 


if(!isset($_SERVER['HTTP_REFERER'])){ 
    header('location:../error.php'); 
    exit; 
}; 
require("../config.php"); 


$new_client = strtolower($_POST['new-offer']); 

$_SESSION['klnm']=$new_client; 

pdf/preview.php

<?php 
session_start(); 

$klient = $_SESSION['klnm']; 
echo $_SESSION['klnm']; 

我的形式kokpit/index.php文件

<form method="post" action="../PDF/index.php"> 
        <input type="text" name="new-offer"> 
        <button type="submit">Wygeneruj</button> 
      </form> 
+0

您是否檢查過'$ _POST ['new-offer']'的值,您是否向'index.php'發出了POST請求? – jeroen

+0

你能告訴我們你的表單嗎? –

+0

它在索引中顯示正常,但它在preview.php中損壞我不明白爲什麼 – michal

回答

0

您還沒有<button 'type'=submit 'name'=submit ..要測試isset($ _ POST ['submit'])以等待用戶輸入...測試會話是否已經啓動也很有幫助(請參閱下面的代碼)避免告示/不可預見的行爲:

的index.php:

<?php 
    session_start(); 

    echo 'in INDEX' . '<br />'; 

    if(!isset($_SERVER['HTTP_REFERER'])){ 
     header('location:../error.php'); 
     exit; 
    }; 

    include("add_client.php"); 

    if(isset($_POST['submit'])) 
    { 
    $_SESSION['klnm']=$new_client; 
    echo '<br />' . 'INDEX output - $SESSION[klnm]: ' . $_SESSION['klnm']; 

    echo "<br /> <br /> <a href='preview.php' > go preview </a>"; 
    } 

    ?> 

<form method="post" action="index.php"> 
     <input type="text" name="new-offer"> 
     <button type="submit" name='submit' >Wygeneruj</button> 
     </form> 

add_client.php:

<?php 
if(!isset($_SERVER['HTTP_REFERER'])){ 
    header('location:../error.php'); 
    exit; 
}; 

//require("../config.php"); 
if(isset($_POST['submit'])) 
{ 
echo '<br />' . 'in ADD_CLIENT, adding the client ' . '<br />'; 

$new_client = strtolower($_POST['new-offer']); 

$_SESSION['klnm']=$new_client; 
} 

preview.php:

<?php 
// check if session already started 
if(!isset($_SESSION)) 
{ 
    session_start(); 
} 

echo '<br />' . 'in PREVIEW' . '<br />'; 

$klient = $_SESSION['klnm']; 
echo '$SESSION[klnm] == ' . $_SESSION['klnm']; 

echo "<br /> <br /> <a href='index.php' > back to index </a>"; 
?> 
+0

據我所知,session_start應該位於文檔的頂部,所以移動的表單並不是我猜的好動作。另外如何添加名稱提交按鈕可以幫助我在我的情況?我可以在add_client中顯示錶單輸入,但它在預覽中不起作用。另外我可以在add_client中設置$ _SESSION ['klnm'] ='some_test',它將在preview.php中顯示得很好,我只有通過會話 – michal

+0

發送的index.php表單值有問題,第一個語句應該是be session_start(); (已編輯),但對於其他問題 - 您無法在preview.php中看到值的原因,是因爲您沒有按照解釋的方式在索引中提交表單提交。 – lovelace

+0

謝謝你的幫助。 – michal