单击提交按钮后显示文本。PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32952674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:17:53  来源:igfitidea点击:

Display text after submit button is clicked. PHP

phphtml

提问by sotirios

I am trying to catch some text from the textbox, to save it in a variable and then post it on my browser if I click the submit button. My code is this:

我试图从文本框中捕获一些文本,将其保存在一个变量中,然后如果我单击提交按钮将其发布到我的浏览器上。我的代码是这样的:

<?php
$var1=$_POST['text1'];
$var2=$_POST['display'];
    if(isset($var2)){
    var_dump( $var1);
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <input type="text" value="gdf" name="text1" id="text1" /><br /><br />

    <input type="submit" id="display" name="display"><br /><br />
    </body>
</html>

I don't know what is wrong. The code on rextester

我不知道出了什么问题。雷克斯特的代码

采纳答案by methhead

For starters you need to implement a form on you HTML page and set the action to a new .php page.

对于初学者,您需要在 HTML 页面上实现一个表单并将操作设置为一个新的 .php 页面。

<form action='display.php' method='post'> 
   *your input fields go here*

Now create a new php page (in this case display.php)

现在创建一个新的 php 页面(在本例中为 display.php)

Declare the variables as you did....

像你一样声明变量......

$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);

then you can simply echo each variable accordingly...

然后你可以简单地相应地回显每个变量......

Final ResultHTML PAGE:

最终结果HTML 页面:

<form action='result.php' method='post'> 
  <input type="text" value="gdf" name="text1" id="text1" /><br /><br />
  <input type="submit" id="display" name="display"><br /><br />
</form> 

result.php

结果.php

<?php 
      $var1 = $_POST['text1']; // create variables of your form elements
      $var2 = $_POST['display']; 

      echo '$var1 . ' ' . $display . '.'; 

?> 

Basically the php page is saying get text1 and display from the form (names) and create variables of them...

基本上php页面是说从表单(名称)获取text1并显示并创建它们的变量......

Then echo (print) these variables on the screen. (in plain english xD)

然后在屏幕上回显(打印)这些变量。(纯英文xD)

p.s Your rextester isn't working because you haven't specified a form.

ps 你的 reextester 不工作,因为你没有指定一个表格。

回答by CONvid19

You need to use a form, otherwise you aren't posting anything.
Something like:

您需要使用form,否则您将不会发布任何内容。
就像是:

form.html

表单.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
<form method="post" action="yourphpfile.php">
        <input type="text" value="gdf" name="text1" id="text1" /><br /><br />
        <input type="text" value="display" name="display" id="display" /><br /><br />
        <input type="submit" value="submit"><br /><br />
</form>
    </body>
</html>

yourphpfile.php

你的phpfile.php

<?php

if(!empty($_POST['text1']) and !empty($_POST['display'])){
   $var1=$_POST['text1'];
   $var2=$_POST['display'];
   echo "$var1 $var2";

}
?>


Check this complete example

检查这个 完整的例子

回答by Twisty

You need to setup the FORMtags so they have the correct attributes for POST. Try:

您需要设置FORM标签,以便它们具有正确的POST. 尝试:

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
            <input type="text" value="gdf" name="text1" id="text1" /><br /><br />
            <input type="submit" id="display" name="display"><br /><br />
        </form>
    </body>
</html>

回答by Ankit

To display text box and button on clicking submit button

单击提交按钮时显示文本框和按钮

<form>
<input type="submit"  value="OK" name="btn_name"/><br />
    <?php
        if(isset($_GET['btn_name']))
           {
               echo "<input type=text name=txt_userinput />";
               echo "<input type=submit value=Area name='btn_calculate'/>";                  
           }
     ?>
</form>