Home > Enterprise >  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 id is wrong or invalid

Time:05-09

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 > 
                <div >
                    <h2 >Tracking Number #: <span >SS-<?php echo $fetched_row['t_number'] ?></span></h2> 
                    <div > 
                        <!--Login Form-->                         
                        <form method="post" action="contact.html"> 
                            <script type="text/javascript"></script>
 
<section > 
    <div > 
        <div > 
            <!--tracking Form-->             
        </div>
        <article >
            <header >
                <div >
                    <div ><span ></span>
                    </div>
                    <ul >
                        <li >
                            <?php echo $fetched_row['marquee'] ?>
                        </li>
                    </ul>
                </div>
            </header>
            <div >
                <h6>Tracking ID: SS-<?php echo $fetched_row['t_number'] ?></h6>
                <article >
                    <div >
                        <div > <strong>Estimated Delivery time:</strong> <br/>
                            <?php echo $fetched_row['date'] ?>
                        </div>
                        <div > <strong>Shipping BY:</strong> <br/> 
                            <?php echo $fetched_row['c_name'] ?>, | <i ></i> 
                            <?php echo $fetched_row['c_number'] ?> 
                        </div>
                        <div > <strong>Status:</strong> <br/> 
                            <?php echo $fetched_row['t_status'] ?> 
                        </div>
                        <div > <strong>Tracking #:</strong> <br/> 
                        SS-<?php echo $fetched_row['t_number'] ?> 
                        </div>
                    </div>
                </article>
            

CodePudding user response:

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.
  • Related