2015-04-03 62 views
0

我想問的可能是愚蠢的問題,但很難找到錯誤,因爲它太簡單了。我想要做的是獲取一個字符串值並在另一個頁面中發佈到值。兩頁的來源如下。問題是,無論我在文本框中輸入什麼內容,當我按下OK按鈕時,結果都是這樣的「;?> 這是我第一次使用實際的服務器,所以......你們能幫我嗎? 關於html表單提交服務器

<html> 
<body> 
    <form method="get" action="id.php"> 
     <input type='text' name='id'> 
     <input type='submit' value='OK'> 
     <input type='reset' value='Cancel'> 
    </form> 
</body> 
</html> 

id.php

<? php 

echo " $id welcome <p>"; 

?> 
+0

我懷疑'<? PHP'也沒有幫助 - 刪除空間。我懷疑第二頁不是通過PHP引擎渲染的(在瀏覽器中使用View Source進行檢查)。 – halfer 2015-04-03 11:10:30

回答

2

你實際上是非常接近

由於您使用method="get"我們將與超全局$_GET

獲得ID

id.php:

<?php 
echo "<p>".htmlspecialchars($_GET['id'], ENT_QUOTES, 'UTF-8'). "welcome </p>"; 
?> 

注意我們用這個來防止爲了保護輸出數據的XSS攻擊, 功能htmlspecialchars,你應該把它展示給用戶的時候逃跑的數據。這可以防止瀏覽器將任何意想不到的意義應用於任何可能被發現的特殊字符序列(例如,用戶未經授權的JavaScript注入)。

+0

@halfer感謝您指出,更新了我的答案。 – Daan 2015-04-03 11:05:41

+0

出色的工作,謝謝。 – halfer 2015-04-03 11:07:15

1
<html> 
<body> 
    <form method="get" action="id.php"> 
     <input type='text' name='id'> 
     <input type='submit' value='OK' name="submit"> 
     <input type='reset' value='Cancel'> 
    </form> 
</body> 
</html> 

在這樣

<?php 

echo $_GET['id']."welcome <p>"; 

?> 
0

更改您的id.php這個id.php使用,

<?php 

if(isset($_GET['submit'])) // checks if submit button is clicked. Prevents accident submit 
{ 

    $get_id = $_GET['id']; 
    echo "Id is : " . $get_id; 
} 

?> 

變化

<input type='submit' value='OK'> 

<input type='submit' value='OK' name="submit">