Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to keep the values in the form after click on submit button with PHP

1 Answer

0 votes
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php

$user_name = $email = $description = $gender = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    if (empty($_POST["user_name"])) 
    {
        $user_name = "";
    } 
    else 
    {
        $user_name = clean_data($_POST["user_name"]);
    }    
    
    if (empty($_POST["email"])) 
    {
        $email = "";
    } 
    else 
    {
        $email = clean_data($_POST["email"]);
    }

    if (empty($_POST["description"])) 
    {
        $description = "";
    } 
    else 
    {
        $description = clean_data($_POST["description"]);
    }

    if (empty($_POST["gender"])) 
    {
        $gender = "";
    } 
    else 
    {
        $gender = clean_data($_POST["gender"]);
    }
}

function clean_data($data) 
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   
   return $data;
}
?>
<h2>Keep The Values in The Form With PHP</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   User Name: <input type="text" name="user_name" value="<?php echo $user_name;?>">
   <br /><br />
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <br / ><br / >
   Description: <textarea name="description" rows="5" cols="40">
                   <?php echo $description;?></textarea>
   <br /><br />
   Gender:
   <input type="radio" name="gender" 
        <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
   <input type="radio" name="gender" 
        <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
   <br><br>
   <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

 



answered Nov 25, 2015 by avibootz
...