php - 点击提交时,我的 php 没有更新 html 文件
问题描述
我检查了以前有类似问题的人的问题。我知道我的 .html 和 .php 文件都在同一个目录中,并且在 xampp 的 htdocs 中。当我点击提交哦我的 html 文件时,它会更改为 php 文件,但它是只读文本或其他内容。我在 xampp 上运行 Apache。
我觉得我的代码还可以,但我这周才开始使用 php。我的教授真的没有帮助。很抱歉,这是一个与作业相关的问题,但如果我能让它工作,我知道我可以解决剩下的问题。
它只是没有更新并向我显示更新。我可以在我的表单中输入数据并点击提交,但我在 php 文件上什么也没有得到,并且 html 文件切换到 php 但它只是文本。我已经尝试了很多,但没有任何效果。很可能我错过了一些代码或一些简单的东西。
非常感谢任何可以提供帮助的人,因为我仍在努力解决这个问题。
<!DOCTYPE html>
<html lang="en">
<head>
<title>My name's Vehicle</title>
<link rel="stylesheet" href="style.css">
<meta charset="utf-8">
</head>
<body>
<header>
<h1>My name's Vehicle</h1>
</header>
<section>
<p>This page will prompt my name for information on her dream vehicle</p>
<h2>Click on the button below to enter new information</h2>
<br>
<!--Using html form to link to php.-->
<form action="index_process.php" method="post">
<label>Vehicle Type:</label>
<input type="text" name="Vehicle"><br><br>
<label>Color: </label>
<input type="text" name="Color"><br><br>
<label>Year: </label>
<input type="text" name="Year"><br><br>
<label>Make: </label>
<input type="text" name="Make"><br><br>
<label>Model: </label>
<input type="text" name="Model"><br><br>
<input type="submit" value="Submit">
</form>
</section>
</body>
</html>
这是我的php
<!DOCTYPE html>
<!--declaring variables-->
<?php
$Vehicle="";
$Color="";
$Year="";
$Make="";
$Model="";
?>
<html lang="en">
<head>
<title>My name's Vehicle</title>
<style><?php include 'style.css'; ?></style>
<meta charset="utf-8">
</head>
<body>
<header>
<h1>My name's Vehicle</h1>
</header>
<section>
<p>This page will prompt my name for information on her dream vehicle</p>
<h2>Click on the button below to enter new information</h2>
<br>
<!--I need to use the post method-->
<form method="post" action="index_process.php">
<label>Vehicle Type:</label> <input type="text" name="Vehicle" value="<?php echo $Vehicle;?>">
<br><br>
<label>Color:</label> <input type="text" name="Color" value="<?php echo $Color;?>">
<br><br>
<label>Year:</label> <input type="text" name="Year" value="<?php echo $Year;?>">
<br><br>
<label>Make:</label> <input type="text" name="Make" value="<?php echo $Make;?>">
<br><br>
<label>Model:</label> <input type="text" name="Model" value="<?php echo $Model;?>">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['Submit'])){
echo("•Vehicle Type: " . $_POST['Vehicle'] . "<br />\n");
echo "<br>";
echo("Color: " . $_POST['Color'] . "<br />\n");
echo "<br>";
echo("Year: " . $_POST['Year'] . "<br />\n");
echo "<br>";
echo("Make: " . $_POST['Make'] . "<br />\n");
echo "<br>";
echo("Model: " . $_POST['Model'] . "<br />\n");
echo "<br>"; }
?>
</section>
</body>
</html>
解决方案
更改if(isset($_POST['Submit']))
php 文件,if($_SERVER['REQUEST_METHOD'] == 'POST')
以便可以显示表单内容。
如果您希望发布的值出现在 php 文件表单的字段上,请在表单之前使用相应的值设置变量,如下面的代码:
<!DOCTYPE html>
<!--declaring variables-->
<?php
$Vehicle="";
$Color="";
$Year="";
$Make="";
$Model="";
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$Vehicle = $_POST['Vehicle'];
$Color = $_POST['Color'];
$Year = $_POST['Year'];
$Make = $_POST['Make'];
$Model = $_POST['Model'];
}
?>
<html lang="en">
<head>
<title>My name's Vehicle</title>
<style><?php include 'style.css'; ?></style>
<meta charset="utf-8">
</head>
<body>
<header>
<h1>My name's Vehicle</h1>
</header>
<section>
<p>This page will prompt my name for information on her dream vehicle</p>
<h2>Click on the button below to enter new information</h2>
<br>
<!--I need to use the post method-->
<form method="post" action="index_process.php">
<label>Vehicle Type:</label>
<input type="text" name="Vehicle" value="<?php echo $Vehicle;?>">
<br>
<br>
<label>Color:</label>
<input type="text" name="Color" value="<?php echo $Color;?>">
<br>
<br>
<label>Year:</label>
<input type="text" name="Year" value="<?php echo $Year;?>">
<br>
<br>
<label>Make:</label>
<input type="text" name="Make" value="<?php echo $Make;?>">
<br>
<br>
<label>Model:</label>
<input type="text" name="Model" value="<?php echo $Model;?>">
<br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo("•Vehicle Type: " . $_POST['Vehicle'] . "<br />\n");
echo "<br>";
echo("Color: " . $_POST['Color'] . "<br />\n");
echo "<br>";
echo("Year: " . $_POST['Year'] . "<br />\n");
echo "<br>";
echo("Make: " . $_POST['Make'] . "<br />\n");
echo "<br>";
echo("Model: " . $_POST['Model'] . "<br />\n");
echo "<br>";
}
?>
</section>
</body>
</html>
推荐阅读
- docker - 如何在 Docker 上运行的 thingboard 中连接 cassandra
- lucene - 可以恢复索引的 Apache Lucene 字段的内容吗
- python - Python - 在列表列表中查找列表
- algorithm - 计算具有特定 XOR 值的子集的数量
- objective-c - 如何修复 MBProgressHUD 大小
- artifactory - Artifactory FileSystemUsage 比 TotalRepositorySize 大得多
- google-tag-manager - 如何使用谷歌标签管理器检查/验证,提交前表单是否为空?
- batch-file - 使用批处理文件删除以相同名称开头的文件夹的多个副本
- php - 未捕获的错误:找不到类“HttpRequest”
- javascript - 对等连接保持在 chrome://webrtc-internals/ 即使在断开连接后