首页 > 解决方案 > 点击提交时,我的 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("&#8226Vehicle 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>

标签: phphtml

解决方案


更改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("&#8226Vehicle 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>

推荐阅读