简体   繁体   English

另一页中的php变量

[英]php variables in another page

im new to php, i just want to echo firstname can anyone please help me ! 我是php的新手,我只想回应firstname,有人可以帮帮我吗!

When i click on the submit button it redirects to action.php and then i want to echo my firstname from the form from previous page. 当我单击提交按钮时,它重定向到action.php,然后我想从上一页的表单中回显我的名字。

Here is my code : 这是我的代码:

action.php action.php的

<?php

    echo "Great! Thanks" . $s1 . " for responding to our survey" ; 

?>

index.php 的index.php

<head>

    <title>Idiot Box Survey</title>
    <meta charset="utf-8">
</head>

<body>

    <form action="action.php" method="post">

        <label>Firstname : </label>
        <input type="text" name="fname"><br><br>

        <label>Lastname : </label>
        <input type="text" name="lname"><br><br>

        <label>Year of birth : </label>
        <input type="date" name="ybirth"><br><br>

        <label>Year at School : </label>
        <input type="date" name="yschool"><br><br>

        <label>When will You Wake up : </label>
        <input type="time" name="wake"><br><br>

        <label>How much time you spend on studying : </label>
        <input type="time" name="study"><br><br>

        <label>How much time you spend on Video games : </label>
        <input type="time" name="vgames"><br><br>

        <label>How much time you spend on tv : </label>
        <input type="time" name="tv"><br><br>

        <label>How much time you spend with family : </label>
        <input type="time" name="family"><br><br>

        <label>How much time you spend with friends : </label>
        <input type="time" name="friends"><br><br>

        <label>When will You Sleep : </label>
        <input type="number" name="sleep"><br><br>

        <button type="submit" name="submit"> Submit Form </button>

    </form>

</body>

您的问题中似乎缺少action.php,但是无论哪种方式,您的名字都将在$_POST['fname']

<?php echo $_POST['fname']?>

使用输入的名称属性来打印其他字段

<?php echo $_POST['lname']?> //for the last name and soo ..

Since my edits are apparently not accepted in other answers. 由于我的修改显然不被其他答案接受。 Instead of calling a non existing php variable in $s1, On your action.php page write this. 而不是在$ s1中调用不存在的php变量,请在您的action.php页面上编写此代码。

if($_POST["submit"]){
    $fname = $_POST["fname"];
    echo "Great! Thanks" . $fname . " for responding to our survey" ;
}

This allows your form to pass the submitted data to the desired script. 这使您的表单可以将提交的数据传递到所需的脚本。 When you hit the submit button, you'll see the value that you put into the fname input field. 当您点击提交按钮时,您将看到输入到fname输入字段中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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