-1

I am currently attempting to create a simple test web page which takes in a user's input of their name through a form and then after the form is submitted, the page is refreshed and the page now ONLY displays "Hello NAME".

Now, after going to this specific URL, you are no longer able to enter the name again and you only see the "Hello NAME" phrase. Also, the page with the form and the final phrase must be reachable by the same URL - they must be the same page.

Any help would be greatly appreciated!

  • Sadly, I have no idea how to even begin coding the backend - I have only coded in HTML and CSS. If you have a small code snippet I can toy with, please do let me know! – Paul Boldyrev Jan 07 '19 at 23:28
  • HTML: set `method` attribute of your `form` tag to `get` instead of `post` (if any). PHP: use `$_GET` or `$_REQUEST` variables to receive data instead of `$_POST`. – user1597430 Jan 07 '19 at 23:31

3 Answers3

0

So from what I understand you want to get a value from $_GET or $_POST.

After it goes to the other url you can set the NAME set by the user on a $_SESSION variable, you just need to start the session at the start of the page session_start() and then add the name into the variable $_SESSION['name'] = $_POST['name'] or $_SESSION['name'] = $_GET['name']; depends on what you want to do or use. And don't forget about htmlentities() and strip_tags() to "sanitize" the variable or to make it more secure to use.

After that you should print something like echo "Hello ". $_SESSION['name']; or anything like that.

SkyLiner
  • 1
  • 3
0

Here's a very basic example of how to do that using a session and post/redirect/get.

<?php
    session_start();  // be sure to start the session first
    // **don't output anything before this**

    // if the name has been added to the session, echo the hello message and exit the script
    // (the form won't be displayed)
    if (isset($_SESSION['name'])) {
        echo 'Hello ' . $_SESSION['name'];
        exit;
    }

    // if the name has been posted, save it to the session and redirect to the same page
    // (the form won't be displayed)
    if (isset($_POST['name'])) {
        $_SESSION['name'] = $_POST['name'];
        header('Location: ' . $_SERVER['REQUEST_URI']);
        exit;
    }
    // If neither of those things are true, go ahead and show the form.
?>


<form method="post">
    <input type="text" name="name">
    <input type="submit" value="Submit">
</form>
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 1) `form` tag with `method="post"` does not properly work without the `name` attribute. 2) why do you think that OP asked for sessions? – user1597430 Jan 07 '19 at 23:46
  • 1
    The OP didn't ask for sessions, I assume because they don't know they exist or what they're called. I thought the goals in the second paragraph of the question indicated that a session was needed (same URL, only hello and no form on subsequent visits after the form has been submitted) – Don't Panic Jan 07 '19 at 23:50
0

Using PHP Sessions is one solution, and another one that you can use is using the GET Parameters.

<form action="page2.php" method="get">
    <input type="text" name="user_name">
    <button type="submit">Submit</button>
</form>

In the example above, what the form will do is submit the entered value (from the user_name input) to access it on page2.php file (this is the form action attribute).

In your page2.php file, you can access the value from the form by using

<?php 
/** $_GET['user_name'] is the name of your input in the form */
echo "Hello " . $_GET['user_name'];
?>

NOTE: $_GET parameters is not recommended when accessing sensitive data, as this value is displayed and can be edited by the end-user in the URL.

sitaw
  • 28
  • 4
  • This looks awesome and thank you for the additional layer of info! Do you know if it would be possible to hide the form after submission? So after the end-user has once submitted it it could never be changed again? – Paul Boldyrev Jan 08 '19 at 03:56
  • @PaulBoldyrev To answer your question if its possible to hide the form after submission, the answer is YES. Although you can do it with only one .php file, I suggest that you follow what I wrote above. For example, what you can do is display the form in page1.php, after submitting the form, it will automatically redirect you to page2.php. From the end-user point-of-view, the form from page1.php will not be shown as the user is already in page2.php view. Try it! :) – sitaw Jan 08 '19 at 08:22