[英]php script isn't working on server while works on localhost [xampp]
[英]Server side php script not working in Xampp
我試圖運行一個簡單的程序,該程序將從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
的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);
}
?>
您沒有從php中的POST數據正確引用表單輸入。
$_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"/>
在這種情況下,HTML中的id
attrib( idone
)僅用於CSS。
它不發布到服務器回來,所以PHP 不看它
服務器看不到來自html的id-js和css的ID。
因此,您需要使用name
參數。
希望能有所幫助;)
您的PHP需要引用輸入字段名稱而不是id。 所以
<?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);
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.