2015-07-01 50 views
-1

我試圖運行一個簡單的程序,將從一個HTML格式,收集用戶的名稱和性別和使用服務器端腳本(PHP)將輸出名稱和性別輸入。這是我的html和php代碼,我正在運行xampp控制面板版本3.2.1。當我使用http://localhost:8080/xampp/test.html從瀏覽器運行它時,頁面將轉到http://localhost:8080/xampp/test.php,但該頁面是空白的。 PHP文件是test.php的和HTML文件test.html的服務器端PHP腳本不工作在Xamppp

HTML

<html> 

<form action="test.php" method="post"> 
<p> 
Name: 
<input type="text" name="fname" id="idone"/> 
</p> 
<p> 
Sex: 
<input type="text" name="sex" id="idtwo"/> 
</p> 
<input type="submit" value="Go" name="submit" id="submit"/> 

</form> 

</html> 

php code 

<?php 
//if the name field is filled in 
if(isset($_POST['idone'])) 
{ 
$name=$_POST['idone']; 
$sex=$_POST['idtwo']; 
printf("hello %s, you are a %s",$name,$sex); 
} 

?> 
+0

也許嘗試HTTP://localhost/test.php並命名您的文件test.php的 – Toumash

+0

php的文件已經命名爲test.php的,和HTML文件名爲test.html的 –

+0

* *危險**:此代碼[易受XSS影響](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS))。用戶輸入需要在插入HTML文檔之前進行轉義! – Quentin

回答

2

你是不是正確引用從POST數據的形式輸入在PHP。
的對象名稱$_POST是那些從name屬性,所以你的情況正確的代碼將是:

if(isset($_POST['fname'])) 
{ 
// make sure you are properly escaping theese values, 
// when you will be dealing with dbs or files 
$name=$_POST['fname']; 
$sex=$_POST['sex']; 
} 

<input type="text" name="fname" id="idone"/>
在這種情況下,id ATTRIB(idone)在HTML將只針對CSS中使用。
它不發佈到服務器回來,所以PHP 看到它

+0

ID屬性不僅用於CSS。它們也用於JS,HTML(例如'label'元素的'for'屬性)和用於操作HTML的其他工具。 – Quentin

1

你的PHP需要引用輸入字段名稱不是IDS。因此,

<?php 
//if the name field is filled in 

if(isset($_POST['fname'])) { 
    $name=$_POST['fname']; 
    $sex=$_POST['sex']; 
    printf("hello %s, you are a %s",$name,$sex); 
} 

?> 
2

服務器不會從html中看到id - 它對於js和css。
因此,您需要使用name參數。
希望幫助;)