简体   繁体   English

我可以在php中获取表单的输入并在同一个php文件中使用它们吗?

[英]Can I get the Input of a form in php and use them in the same php file?

<?php

    echo '<form action="index3.php "method="get">
        Name: <input type="text" name="fname"/>
        <br/>
        Surname: <input type="text" name="fsur"/>
        <br/>
        Phone: <input type="number" name="phone"/>
        <br/>
        Email: <input type="email" name="email"/>
        <input type="submit" value="Go">         
    </form>';

    $name = $_GET["fname"];
    $surname = $_GET["fsur"];
    $phone = $_GET["phone"];        
    $email = $_GET["email"];

    echo "Hello " . $fname . " " . $fsur . ". You will soon get a confirmation email at: " . $email . " and a confirmation message at this number: " . $phone;


    ?>

I am trying to store into variables the input (fname,fsur,phone,email) of an echoed form in a php code to use them in the same php file and print them as shown below. 我试图在php代码中将回显表单的输入(fname,fsur,phone,email)存储到变量中,以便在同一个php文件中使用它们并打印它们,如下所示。 When i run the code i get these errors in my browser. 当我运行代码时,我在浏览器中收到这些错误。 I use firefox. 我用firefox。 Is there any way I can do that? 有什么方法可以做到吗?

Yes you can do it straight like this - 是的,你可以这样直接做 -

<form action="" method="get">
    Name: <input type="text" name="fname"/>
    <br/>
    Surname: <input type="text" name="fsur"/>
    <br/>
    Phone: <input type="number" name="phone"/>
    <br/>
    Email: <input type="email" name="email"/>
    <input type="submit" name="my_submit" value="Go">         
</form>

<?php
/* This check is needed to make sure that the form is submitted 
 * Otherwise it'll produce error like - undefined index in $_GET
 */
if(isset($_GET['my_submit'])) { 
    $name = $_GET["fname"];
    $surname = $_GET["fsur"];
    $phone = $_GET["phone"];
    $email = $_GET["email"];

    echo "Hello " . $name . " " . $surname . ". You will soon get a confirmation email at: " . $email . " and a confirmation message at this number: " . $phone;
}
?>

See how you can check form parameter 了解如何检查表单参数

<?php

        echo '<form action="" method="get">
            Name: <input type="text" name="fname"/>
            <br/>
            Surname: <input type="text" name="fsur"/>
            <br/>
            Phone: <input type="number" name="phone"/>
            <br/>
            Email: <input type="email" name="email"/>
            <input type="submit" name="submit_form" value="Go">         
        </form>';

        if(isset($_GET['submit_form'])){
            $name = $_GET["fname"];
            $surname = $_GET["fsur"];
            $phone = $_GET["phone"];        
            $email = $_GET["email"];

            echo "Hello " . $name . " " . $surname . ". You will soon get a confirmation email at: " . $email . " and a confirmation message at this number: " . $phone;
        }

        ?>

Note: Notice the name="submit_form" on the submit button and if(isset($_GET['submit_form'])){ and for last echo statement put $name instead of $fname and put $surname . 注意:请注意提交按钮上的name="submit_form"if(isset($_GET['submit_form'])){和最后一个echo语句将$name而不是$fname放入$surname

Instead of using echo you can directly use HTML form outside of tag. 您可以直接在标记之外使用HTML表单,而不是使用echo。 and don't specify action, do <form action="" method="get"> 并且不指定动作,执行<form action="" method="get">

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

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