Home > Enterprise >  My frontend prints out both "Category successfully added" and "Category successfully
My frontend prints out both "Category successfully added" and "Category successfully

Time:10-27

<main>
        <section >
            <h1 >
                Add Category
            </h1>
        </section>
        <section >
        <?php
            if(isset($_GET['error']))
            {
                echo '<font color="red">'.$_GET['error'].'</font>';
                echo '<br><br>';
            }
                                
            if(isset($_GET['ok']))
            {
                echo '<font color="blue">Category successfully added!</font>';
                echo '<br><br>';
            }
                            
        ?>
        </section>
        <section >
            <form action="addcategory_steps.php" method="post">
                <div >
                    <div >
                        <h1 >
                             Category Name
                        </h1>
                        <input type="text" name="category_name" id="" placeholder="category_name">
                    </div>
                    
                    <div >
                        <h1 >
                             Category Image
                        </h1>
                        <input type="text" name="category_image" id="" placeholder="category_image">
                    </div>
                    
                </div>
                <input type="submit" value="Add Category" >
            </form>
        </section>

        <main>
        <section >
            <h1 >
                Delete Category
            </h1>
        </section>
        <section >
        <?php
            if(isset($_GET['error']))
            {
                echo '<font color="red">'.$_GET['error'].'</font>';
                echo '<br><br>';
            }
                                
            if(isset($_GET['ok']))
            {
                echo '<font color="blue">Category successfully deleted!</font>';
                echo '<br><br>';
            }
                            
        ?>
        </section>
        <section >
            <form action="deletecategory_steps.php" method="post">
                <div >
                    <div >
                        <h1 >
                             Category Name
                        </h1>
                        <input type="text" name="category_name" id="" placeholder="category_name">
                    </div>
                    
                    <div >
                        <h1 >
                             Category Image
                        </h1>
                        <input type="text" name="category_image" id="" placeholder="category_image">
                    </div>
                    
                </div>
                <input type="submit" value="Delete Category" >
            </form>
        </section>

Above is my code for my php project, but the frontend prints out both successfully deleted and added. How do I make it only to print successfully added if I press the "Add Category" button and only prints out successfully deleted if I press the "Delete Category" button? I have inserted the screenshot of the problem below.

Error in frontend

CodePudding user response:

There are so many things wrong that the comment is inadequate

  1. Your forms doing a post, so you have to use the $_POST not $_GET
  2. You are not setting $_GET['error']
  3. You need to use the name attribute for the input/button

<input type="submit" value="Add Category" name="addcat">

<input type="submit" value="Delete Category" name="delcat">

  1. Then check which button has been pressed and execute the appropriate code.

if isset($_POST['addcat']) or if isset($_POST['delcat'])

  1. Depending on the code not posted, you may need separate error variables such as $erroradd and $errordel.
  • Related