2016-05-01 19 views
0

我剛開始學習php和html,我想做一些獲取輸入並用php代碼打印輸出的東西。但是,使用我擁有的代碼,它可以讓我輸入,但是當點擊提交時不會打印任何內容。爲什麼?如何使用php代碼打印輸入

<html> 
<head><title>Noob</title></head> 
<body> 
<form action="" method="post"> 
<input type="text" name="username" value="" /> 
<input type="submit" name="submit value="Submit" /> 
</form> 

<?php 
if (isset($_POST['submit'])) { //to check if the form was submitted 
$username= $_POST['username']; 
echo $username; 
} 
?> 

</body> 
<html> 

回答

0

有一個語法錯誤,你已經錯過了雙引號的名稱=「提交」

<?php 
    if (isset($_POST['submit'])) { //to check if the form was submitted 
    // For printing whole post data 
    echo '<pre>'; print_r($_POST); 
    $username= $_POST['username']; 
    echo $username; 
    } 
    ?> 

您也可以用var_dump打印出來。 希望這會幫助你。

3

在下面的一行代碼中的

<input type="submit" name="submit value="Submit" /> 

字NAME =「提交沒有結束的雙冒號

+0

謝謝你的!但是,在輸入我的數據並按提交後,頁面只是刷新並且不打印數據。這是爲什麼? – Joefa

+0

@Joefa用

。「方法= 「POST」> – Sebastian

1

你缺少HTML,你必須

就行了引號
<input type="submit" name="submit value="Submit" /> 

應該

<input type="submit" name="submit" value="Submit" /> 
1

你錯過了按鈕名稱引號

<html> 
<head><title>Noob</title></head> 
<body> 
<form action="" method="post"> 
<input type="text" name="username" value="" /> 
<input type="submit" name="submit" value="Submit" />// you missed here 
</form> 

<?php 
if (isset($_POST['submit'])) { //to check if the form was submitted 
$username= $_POST['username']; 
echo $username; 
} 
?> 

</body> 
<html> 
0

你確定你通過PHP解釋器中運行的頁面? 在本地你可以使用xampp或wampp(windows的產品) 在mac中可以使用mampp