0

im new to php, i just want to echo firstname can anyone please help me !

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.

Here is my code :

action.php

<?php

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

?>

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>

2
  • 2
    I think you are missing the code for action.php? Commented Oct 25, 2014 at 0:57
  • What is the Code im missing ? @Bijan Commented Oct 25, 2014 at 1:01

3 Answers 3

1

action.php appears to be missing from your question, but either way, your first name will be in $_POST['fname']

Sign up to request clarification or add additional context in comments.

1 Comment

Add something that lets him/her know to check to make sure the submit button has the value of post. Review my edit please.
0
<?php echo $_POST['fname']?>

use the name attribute of the input to print the other fields

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

Comments

0

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.

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.

5 Comments

Not sure why I got a down vote for this answer, seeing as how it's a correct and valid answer, but whatever.
The down-vote is because this answer has two typos that will prevent your code from executing. Correct them and I'll happily undo the vote. The edits were rejected (not by me, but I agree with them) because they didn't actually add anything to the answer.
Typos are fixed, thanks for letting me know about them. I know my edit didn't really add to your answer, but I figured that since the OP is new to PHP, he/she would like to see what to do to get the variables when the form is submitted
Down-vote undone. I totally get that. I'm tempted to add tips and pointers like that too all the time, but we're technically supposed to stick to the questions. Otherwise we might as well write the whole application for them. :D
Thanks for removing it. I've got a bad habit of doing exactly that in writing an application to help people. I should probably stop doing that.

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.