2013-11-26 76 views
-1

有一個頁面:login.php如何在php頁面之間傳輸數據?

我嘗試通過窗體接收用戶名和密碼。

的login.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
     <title>Giris Yap</title> 
    </head> 

    <body> 
     <form id="form1" name="form" method="post" action="welcome.php"> 
      <table width="275" border="1"> 
       <tr> 
        <td width="103">user; name: </td> 
        <td width="156"><label> 
          <input type="text" name= "name" /> 
         </label></td> 
       </tr> 
       <tr> 
        <td>password:</td> 
        <td><label> 
          <input type="password" name="textfield2" /> 
         </label></td> 
       </tr> 
      </table> 
      <p><label> 
        <input type="submit" name="Submit" value="submit" /> 
       </label> 
       <label> 
        <input type="reset" name="Submit2" value="reset" /> 
       </label> 
      </p> 
     </form> 
     <p>&nbsp;</p> 
     <p>&nbsp;</p> 

    </body> 
</html> 

之後,我要帶領我的網頁到的welcome.php並表示歡迎......名字......但它不工作。誰能幫我。

的welcome.php:

<?php 
$name = $_REQUEST['name']; 
echo "Welcome"$name; 
?> 
+0

修復你的語法錯誤:'回聲 「歡迎光臨」。 $名稱;'。 –

回答

0

嘗試這樣的。

始終在這方面使用isset()結構。因爲我們知道您的<form>方法有動作後,你可以利用$_POST代替$_REQUEST

<?php 
if(isset($_POST['name'])) 
{ 
    $name = $_POST['name']; 
    echo "Welcome $name"; 
} 
?> 

而且,你缺少你的<form>提交按鈕的代碼,做這樣的

<form id="form1" name="form" method="post" action="welcome.php"> 
    <table width="275" border="1"> 
     <tr> 
      <td width="103">User; Name: </td> 
      <td width="156"><label> 
        <input type="text" name= "name" /> 
        <input type="submit" name="submit" /> 
       </label></td> 
     </tr> 
</form> 
+0

我是否需要將任何php cpde添加到login.php表單頁面?因爲在login.php中沒有php代碼。 – Behzat

+0

@ user3037527,您需要添加提交按鈕,請參閱編輯的代碼。在這種情況下,您的login.php中不需要PHP代碼。 –

+0

我已經編輯如上的login.php代碼。但在這個頁面上沒有任何關於我需要在login.php頁面中添加一些php代碼嗎? – Behzat

0

這一切都取決於您在表單中指定的方法。如果您的形式method="post",那麼你需要找回喜歡$_POST['name']

,如果你的表格method="GET"那麼你需要找回像$_GET['name']

$name = $_POST['name']; // you have used method='post' 
echo "Welcome" .$name; 

此外,$_REQUEST - 的關聯數組默認包含$ _GET內容,$ _POST和$ _COOKIE。不要忘記,因爲$ _REQUEST是一個不同於$ _GET和$ _POST的變量,它在PHP中被視爲是這樣 - 在運行時修改$ _GET或$ _POST元素不會影響$ _REQUEST中的ellements,反之亦然。

e.g:

<?php 

$_GET['foo'] = 'a'; 
$_POST['bar'] = 'b'; 
var_dump($_GET); // Element 'foo' is string(1) "a" 
var_dump($_POST); // Element 'bar' is string(1) "b" 
var_dump($_REQUEST); // Does not contain elements 'foo' or 'bar' 

?> 

編號:http://php.net/manual/en/reserved.variables.request.php

0

你可以試試。

<?php 
    if(isset($_POST['name'])) 
    { 
     $name = $_POST['name']; 
     echo "Welcome ". $name; 
    } 
?> 
0

嘗試

?php 
if(isset($_POST['name'])) 
{ 
    $name = $_POST['name']; 
    echo "Welcome $name"; 
} 
else 
{ 
    echo "Name not set"; 
} 
?> 
0

試試這個:

<?php 
// let us make sure we have a post value. we will use isset to make sure it's set. 
if (isset($_POST['name'])) { 
// you should scrub the $name value before displaying. Never display or work with 
// raw post values. 
$name = $_POST['name']; 
echo "Welcome "; 

// what if they didn't really put in a name? 
// maybe only show it if it's longer than 2? 
if (strlen($str) > '2') 
echo $name; 
} 
} 
else { 
// we don't have a post value? You should redirect with error message or something. 
} 

?>