简体   繁体   中英

Showing error message on correct page in PHP

I have 4 forms in my PHP Project. Index.php will store the user's name and id number then they may click next and it will take them to Form2.php. Form2.php will store some random answers of theirs, Form3.php will do the same as Form2 and Form4.php will store a few details then the user can click submit and the record should save in my DB. The issue I am having is that my ID number field is a unique field, and I want an error to show on Index.php when the user clicks Next if the ID input is the same as one in the DB. Currently, it is showing after the submit button is clicked in the last Form. Is there any way to do this?

Index.php

<body>
    <center>
        <div class="div2">
            <h1>Welcome</h1>
            <form action="form2.php" method="post">
                <p>
                    <label for="firstName">Named:</label>
                    <input size="30" class="rounded-input" type="text" name="name" id="name" autocomplete="off" required>
                </p>
                <p>
                    <label for="lastName">S ID:</label>
                    <input size="30" class="rounded-input" type="text" name="Sid" id="Sid" autocomplete="off" required>
                </p>
                <input class="btn" type="submit" value="Next" style="float: right;">
            </form>
        </div>
    </center>
</body>

Form2.php:

<?php
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['Sid']  = $_POST['Sid'];
?>
<div class="div2">
    <h1>How disappointed would you be if this product ceased to exist?</h1>
    <form action="form3.php" method="post">
        <input type="radio" style="height:20px; width:20px;" required 
                name="product_exist_satisfaction" 
                <?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Very disappointed") echo "checked"; ?> 
                value="Very disappointed">
        <label style="font-size: 20px;"> Very disappointed</label><br />

        <input type="radio" style="height:20px; width:20px;" required 
                name="product_exist_satisfaction" 
                <?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Mildly disappointed") echo "checked"; ?> 
                value="Mildly disappointed">
        
        <label style="font-size: 20px;"> Mildly disappointed</label><br />

        <input type="radio" style="height:20px; width:20px;" required 
                name="product_exist_satisfaction" 
                <?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Not at all") echo "checked"; ?> 
                value="Not at all">
        <label style="font-size: 20px;"> Not at all</label><br />
        <input type="button" onclick="history.back()" 
                value="Previous" style="float: left;">
        <input type="submit" value="Next" style="float: right;">
    </form>
</div>

Insert.php

<?php
session_start();
?>
<body>
    <div class="div2">
<?php
    $conn = mysqli_connect("localhost", "root", "", "survey");
    if ($conn === false) {
        die("ERROR: Could not connect. "
                . mysqli_connect_error());
    }
    $stmt = $conn->prepare('insert into `cus_survey` 
                    ( `fullname`, `Sid`, `product_exist_satisfaction`,
                      `system_battery_runout`, `rank_appliances` ) 
                values (?, ?, ?, ?, ?)');
    $stmt->bind_param('sssss', $_SESSION['fullname'], $_SESSION['Sid'], 
                               $_SESSION['product_exist_satisfaction'], 
                               $_SESSION['system_battery_runout'], 
                               $_POST['rank_sequence']);
    $stmt->execute();
    $msg = ($stmt->affected_rows == 1) 
                ? 'Your survey was captured successfully. Thank You!'!' 
                : 'Sorry, your S ID is used already, Please use another and resubmit.' . "<h3><a href='/index.php'>Click here to edit your S ID</a></h3>" . mysqli_connect_error();
    $stmt->close();
    $conn->close();
    printf('<h3>%s</h3>', $msg);
?>
    </div>
</body>

I have not checked the code, but the basic steps are:

-In your index.php, you could post the index to self, by changing to action="<?php echo $_SERVER['PHP_SELF']; ?>"

-You would then run a select query for the Sid

-If Sid is not already in sql table, then you would redirect to form2.php

-else you have Sid already, then set the error message and then display of the index form with the error message.

<?php
// Start/resume sessions
if (session_status() !== PHP_SESSION_ACTIVE) {
  session_start();
}

//set your error message to empty string
$error_message = "";

// ensure that you actually have values to check
if ((isset($_POST['name'])) &&(isset($_POST['Sid'])))  {


// use select statement to verify that the Sid is not already in table

$sql_check = 'SELECT Sid FROM cus_survey...
//...
//... put your check on the result of select statement

if (Sid not already there) {

   // redirect to form 2
   header("Location: form2.php");   
}else{
    // If Sid already used found, populate the error message, which will get displayed in your body
    $error_message = "Sorry, your S ID is used already, Please use another and submit.";

}
}
?>

<body>
    <center>
        <div class="div2">
        <?php if ($error_message != ""){
            ?>          
            <h1>Oops: <?php echo "{$error_message}"; ?></h1>
            <?php
            }else{
            ?>
            <h1>Welcome</h1>
            <?php
            };
            ?>
            
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <p>
                    <label for="firstName">Named:</label>
                    <input size="30" class="rounded-input" type="text" name="name" id="name" autocomplete="off" required>
                </p>
                <p>
                    <label for="lastName">S ID:</label>
                    <input size="30" class="rounded-input" type="text" name="Sid" id="Sid" autocomplete="off" required>
                </p>
                <input class="btn" type="submit" value="Next" style="float: right;">
            </form>
        </div>
    </center>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM