How to make a form sticky with a function?

I am normally doing like this way to sticks user value into the form

 if (isset($_POST['user_firstname'])){
                    echo $_POST['user_firstname'];};

Is there any other way thr functions or some similar methods?

Hello, you want to stick your form with CSS or Javascript (Jquery) ?

I haven’t learned Javascript, so if CSS is might be useful but I want to check the Javascript code too, if possible.

What exactly do you mean by “sticky”?
The usual meaning with regard to web-page elements is a way of positioning (with css/javascript) an element, which I think is how @lee79 is taking it.
But by your code and this being in PHP, I think you may mean something else.

I mean to say the text which user entered should be stick even after submitted the form or in case the user doesn’t fill the required fields and clicked on submit, so the user doesn’t require to rewrite all the fields again.

1 Like

Generally as part of the form processing the $_POST['data'] will get passes to a variable, usually during sanitisation and validation.
One method I have seen used, to avoid having to use isset and unduly pollute your html with too much logic, is for people to set those variables to empty strings near the start of the script:-

$user_firstname = '';

Later in the actual processing the variable may be set to a value:-

$user_firstname = mySanitiseFunction($_POST['user_firstname']);

That way you can safely <?php echo $user_firstname ?> without having to worry if it is set or not, without the extra logic of the isset condition.
Note that when echoing out user input to html, it’s best to encode it, as in: htmlspecialchars($user_firstname), though this may be done prior to the echo to further separate your logic.

1 Like

I think OP is talking about $_SESSION. They want to have the user data still intact in case of mistakes and what not. They want to store temporary data so that the user doesn’t have to fill out the fields again when the users forget to fill in a field on validation.


“sticky” is also the wrong choice of words. In general web based languages, “sticky” typically relates to either CSS or Javascript more so CSS as CSS can have a “fixed” position to have a “sticky” look on the appearance side of the page.

1 Like

Please see if this link is of any use to use →

Here is an example using @spaceshiptrooper 's $_SESSION suggestion. Basically store posted data in your session and then retrieve it when you come back.

<?php
// sticky.php
error_reporting(E_ALL);
session_start();

// Initialize form data, use an array to help with more complex forms
$formData = [
    'last_name' => 'Trump',
];
// Merge in any session data
if (isset($_SESSION['formData'])) {
    $formData = array_replace($formData,$_SESSION['formData']);
}
// Check for posting
if ( $_SERVER['REQUEST_METHOD'] === 'POST') {

    // Pull the data
    $formData['last_name'] = filter_var(trim($_POST['last_name']),FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES);

    // Save in session
    $_SESSION['formData'] = $formData;

    // Redirect to clear the post
    header('Location: sticky.php');
    exit();
}
// Output the form
$last_name = htmlspecialchars($formData['last_name'],ENT_COMPAT | ENT_HTML5,'UTF-8');

$html = <<< EOT
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Sticky Form</title>
</head>
<body>
    <form method="POST" action="sticky.php">
        <label for="last_name">Last Name</label>
        <input id="last_name" name="last_name" type="text" value="{$last_name}"/>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>
EOT;
    echo $html;

Thanks for the code, could you please tell me why you have used

if ( $_SERVER[‘REQUEST_METHOD’] === ‘POST’) { instead of isset, recently I have opened the topic regarding this question, but as you have used, so you will better know the reason as well :smiley:

That is the correct method to test if a form has been submitted (via post method).
Use isset only to test for specific form fields.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.