'How do i display a error message or redirect a user to a page if tracking id is wrong or invalid
How do i display a error message or redirect a user to a page if tracking number entered is wrong or does not exit in database.
i have a form where user enters a tracking code and the form is sent through post to php tracking page. i want to return the user back to the form page with an error message when the tracking number entered is wrong. here is my code thank you.
<?php
if(isset($_POST['trackid']))
?>
<?php
$sql_query="SELECT * FROM tracking WHERE t_number=".$_POST['trackid'];
$result_set=mysqli_query($conn,$sql_query);
$fetched_row=mysqli_fetch_array($result_set,MYSQLI_ASSOC);
?>
<section class="quote-section">
<div class="auto-container">
<h2 class="text-center text-danger">Tracking Number #: <span class="text-success">SS-<?php echo $fetched_row['t_number'] ?></span></h2>
<div class="quote-form-box">
<!--Login Form-->
<form method="post" action="contact.html">
<script type="text/javascript"></script>
<section class="quote-section">
<div class="auto-container">
<div class="quote-form-box">
<!--tracking Form-->
</div>
<article class="card">
<header class="card-header">
<div class="bg-success border-success shadow ticker-wrapper-h">
<div class="heading"><span class="fa fa-bullhorn icon"></span>
</div>
<ul class="news-ticker-h">
<li class="font-weight-normal text-light">
<?php echo $fetched_row['marquee'] ?>
</li>
</ul>
</div>
</header>
<div class="card-body">
<h6>Tracking ID: SS-<?php echo $fetched_row['t_number'] ?></h6>
<article class="card">
<div class="card-body row">
<div class="col"> <strong>Estimated Delivery time:</strong> <br/>
<?php echo $fetched_row['date'] ?>
</div>
<div class="col"> <strong>Shipping BY:</strong> <br/>
<?php echo $fetched_row['c_name'] ?>, | <i class="fa fa-phone"></i>
<?php echo $fetched_row['c_number'] ?>
</div>
<div class="col"> <strong>Status:</strong> <br/>
<?php echo $fetched_row['t_status'] ?>
</div>
<div class="col"> <strong>Tracking #:</strong> <br/>
SS-<?php echo $fetched_row['t_number'] ?>
</div>
</div>
</article>
Solution 1:[1]
You can do this with sessions. Change the beginning of your code like this:
<?php
if(isset($_POST['trackid'])) {
$sql_query="SELECT * FROM tracking WHERE t_number=".$_POST['trackid'];
$result_set=mysqli_query($conn,$sql_query);
$fetched_row=mysqli_fetch_array($result_set, MYSQLI_ASSOC);
if (mysqli_num_rows($result_set) === 0) {
header('Location: <your form link here>');
session_start();
$_SESSION['error'] = 'Error message';
exit;
}
}
?>
So in your form page you add:
<?php
session_start();
if (isset($_SESSION['error'])) {
echo $_SESSION['error'];
unset($_SESSION['error']);
}
?>
Basically, on your view page it is being checked if there was a return of any row in the query, if not, it does the following:
- Add a header to redirect to the form page;
- Starts a session;
- Saves the error message in the session variable;
- Closes the script.
The following is happening on your form page:
- Start of session;
- Checks if the key with the error message exists;
- If it exists, it displays a message and erases the session variable index.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lucas |
