Logic right but variable do not stay on error, status line?

<?php
//Logic php is re-entrant. User enters data, db good, fields reset and message on status line.
//can enter again, have to add button to return to main menu.
//if db error, display error on status line, leave data intact.

//The form part is not display anything.

session_start();
error_reporting(E_ALL); // ALL KINDS OF ERROR
// (C) DISPLAY ERROR MESSAGES
ini_set("display_errors", 1);

require("db.php"); 
include("config.php");

if ($_SESSION["onlinestat"] == "0")
{
    $_SESSION["onlinestat"] ="6";
}
else
{

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$phone = $_POST['phone'];
$email = $_POST['email'];

try {
$stmt = $pdo->prepare("INSERT INTO contactstest (firstname, lastname, phone, email, address, city, state, zipcode) VALUES ('$fname','$lname','$phone','$email','$address','$city','$state','$zipcode')");
}
catch (Exception $e) {
    // MySQL exception
    $_SESSION["onlinemessage"] = $e->getMessage();
    $_SESSION["onlinestat"] = "9";
}
try
{
$stmt->execute();
}
catch (Exception $e) {
    // MySQL exception
    $_SESSION["onlinemessage"] = $e->getMessage();
    $_SESSION["onlinestat"] = "9";
}

$where = strpos($_SESSION["onlinemessage"],":");
if (!$where)
{
    $_SESSION["onlinemessage"] = 'Thank You For your vote to oppose the Project';
    $fname=$lname=$address=$city=$state=$zipcode=$phone=$email="";
    $_SESSION["onlinestat"] = "1";
}
else
{
$len = strlen($_SESSION["onlinemessage"]) - $where;
$_SESSION["onlinemessage"] ="*ERROR* " . substr($_SESSION["onlinemessage"],$where+1,$len);
}
$status = $_SESSION["onlinemessage"];
}
?>
<html>
<head>
    
<style>
body {background-color: #008B8B;}
h3 {text-align: center;}
.error {color: #FF0000;}
 .container {
            width: 1000px;
            margin: 50px auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

</style>

</head>
<body>
    <div class="container">
        
        <form action="/registertest/index.php" method="POST">
            <p>
            <label for="fname">First name:</label>
            <input type="text" id="fname" size="40" name="fname" required>
            <label for="lname">Last name:</label>
            <input type="text" id="lname" size="40" name="lname" required>
            </p>
            <p>
            <label for="address">Address:&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="text" id="address" size="40" name="address" required>
            </p>
            <p>
            <label for="city">City:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="text" id="city" size="40" name="city" required>
             <label for="state">State:</label>
            <input type="text" id="state" sie="20" name="state" required>
             <label for="zip">Zipcode:</label>
             
            <input type="text" id="zip" size="5" name="zipcode" maxlength="5" required>
            </p>
            <p>
            <label for="email">Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="email" id="email" size="50" name="email">
            </p>
            <p>
            <label for="phone">Phone:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="tel" id="phone" size="10" name="phone" maxlength="10">
            </p>
            <label for="status">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="text" id="status" size="72" name="status">
            </p>
            
            <input type="submit" value="Submit" text-align: center; >
            <input type="reset" value="Reset">
        </form>
    </div>      
</body>

</html>

To post code on the forum, either put bbcode [code][/code] tags or markdown three back-ticks ``` on their own lines, above and below your code. I have edited your post above.

The page reloads and youre not redisplaying the POST values.

    $_SESSION["onlinestat"] ="6";
}

This makes zero sense without context. I don’t get what is 6

just used to force 1st time thru. set to 0, then on 1st pass to 6. Re-entrance means data is populated, do the insert. set 1 is success 9 means error in mysql.

I see. That makes sense now. Mebby you can save them in constants and add comment to explain the lifecycle. This very good approach for future in case you or someone else looking at the code.

// comment to explain
const STAT_INITIAL      = '0'; 
const STAT_FIRST_PASS   = '6';

Did you manage to get the form work as intended yet?

Sponsor our Newsletter | Privacy Policy | Terms of Service