1
<?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. When i run the code i get these errors in my browser. I use firefox. Is there any way I can do that?

4
  • Yes but you need to check if isset some parameter from the form. Commented Mar 1, 2017 at 17:58
  • Yes, but remember the life cycle of a script with a form in it. The first time you run the script, the user will not have submitted the form, so NONE of those $_GET variables will exist, and your code will throw errors. So you must somehow test that they do exists before using them Commented Mar 1, 2017 at 18:00
  • You must send your form if you wont take inputs values in php And if you wont to write code in same file you have to skip write this file name into your form action attribute Commented Mar 1, 2017 at 18:00
  • Thank you for your help! It is just a template I am working on! Thank you for your advises and for your interest to help me all!! Commented Mar 1, 2017 at 18:28

2 Answers 2

2

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;
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

!empty($_GET) method is not good practice. Suppose this is test.php and if you call this file by passing some parameter as http://yourhost/test.php?test=123 you will get notices. Try out once and you will get clear picture. Thats why its better to check the form element and activate some PHP code.
@Vpcont this will not solve the problem. Please approve edits so that other can get good answer. I have changed your code
You welcome bud, would you please correct some typo on if statement, there are two if. Please
1

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.

Instead of using echo you can directly use HTML form outside of tag. and don't specify action, do <form action="" method="get">

1 Comment

Thank you very much as well! This solution also works perfectly and it is inside php code as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.