繁体   English   中英

服务器端php脚本在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM