drvfqr5609 2018-11-01 02:35
浏览 37
已采纳

我的php在点击提交时没有更新html文件

I have checked on previous questions of people who have a similar issue. I know that both my .html and .php file are in the same directory and in htdocs of xampp. When I click submit oh my html file it changes to the php file but it's read only text or something. I'm running Apache on xampp.

I feel like my code is ok but I just started php this week. My professor is not helping really. I'm sorry this is a assignment related question but if I could get it to work I know I can fix the rest of it.

It's just not updating and showing me the update. I can enter data into my form and hit submit but I get nothing on the php file and the html file switches to php but it's just text. I've tried quite a lot and nothing is working. Most likely I'm missing some code or something simple though.

Thanks so much to anyone who can help as I'm at a standstill still trying to figure this out.

<!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>

Here's my 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 />
");
echo "<br>";
echo("Color: " . $_POST['Color'] . "<br />
");
echo "<br>";
echo("Year: " . $_POST['Year'] . "<br />
");
echo "<br>";
echo("Make: " . $_POST['Make'] . "<br />
");
echo "<br>";
echo("Model: " . $_POST['Model'] . "<br />
");
echo "<br>"; }
?>

    </section>

    </body>
</html>
  • 写回答

2条回答 默认 最新

  • dongtaogou6226 2018-11-01 02:57
    关注

    Change if(isset($_POST['Submit'])) in the php file to if($_SERVER['REQUEST_METHOD'] == 'POST') so that the form content can be shown.

    If you want the posted values to appear on fields of the php file's form, set the variables with the respective values before the form like the following code:

    <!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 />
    ");
                echo "<br>";
                echo("Color: " . $_POST['Color'] . "<br />
    ");
                echo "<br>";
                echo("Year: " . $_POST['Year'] . "<br />
    ");
                echo "<br>";
                echo("Make: " . $_POST['Make'] . "<br />
    ");
                echo "<br>";
                echo("Model: " . $_POST['Model'] . "<br />
    ");
                echo "<br>";
            }
            ?>
    
        </section>
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。