On click button I am redirected to another page

Hi,

I have created from following a guide the following script which on click of a button should add the product to the basket and refresh the page, but instead refreshes and directs to the default unknown product page as highlighted in bold - is there anyway that I can remain on the current page?

<?php

    $item_id = $_GET['item_id'] ?? **1;**

    foreach ($product->getData() as $item) :

        if ($item['item_id'] == $item_id) :

?>

Since the posted code is incomplete, we cannot possibly tell you why it’s redirecting.

However, you should use a post method form for anything that creates or changes data on the server. You should only use a link/get method form for determining what will be displayed on a page.

It also appears that you are getting and looping over all possible products to test if the submitted item id exists. Don’t do this. If you have 10,000’s of products would doing that make sense? You would need to query to directly find if the submitted item id exists.

Also, setting a default item id of 1 is not the proper operation for an ‘add to cart’. If there is no submitted item id, or it’s not a positive integer greater than zero, that’s an error and would be due to either a programming mistake or someone/something submitting their own values.

Thank you for this. I followed various tutorials to get to this point. I’m just finding this really difficult to grasp and hope it will (eventually) become clearer. The full code is below, it’s probably a complete mess and makes little sense. I really appreciate the feedback you have given. The post method is used initially to find what to display, but on the click of add to basket I then get thrown to another page.

<!--   product  -->

<?php

    $item_id = $_GET['item_id'] ?? 1;

    foreach ($product->getData() as $item) :

        if ($item['item_id'] == $item_id) :

?>

     

            <section id="product" class="py-3">

                <div class="container">

                    <div class="row">

                        <div class="col-sm-6">

                            <img src="<?php echo $item['item_image'] ?? "./HTML Template/assets/Images/3200G Stock Image.png" ?>" alt="product" class="img-fluid py-4">

                            <div class="form-row pt-4 font-size-16 font-baloo">

                                <div class="col">

                                    <button type="submit" class="btn btn-danger form-control">Proceed to Buy</button>

                                </div>

                                <div class="col">

                                <form method="post">

                                    <input type="hidden" name="item_id" value="<?php echo $item['item_id']; ?>">

                                    <input type="hidden" name="user_id" value="<?php echo 1; ?>">

                            

                            <?php

                            if (in_array($item['item_id'], $Cart->getCartId($product->getData('cart')) ?? [])){

                                echo '<button type="submit" disabled class="btn btn-success font-size-14 form-control">In the Cart</button>';

                            }else {

                               echo '<button type="submit" name="top_sale_submit" class="btn btn-warning form-control">Add to Cart</button>';

                            }

                            ?>

                                    </form>

                                </div>

                            </div>

                        </div>

                        <div class="col-sm-6 py-2">

                            <h5 class="font-baloo font-size-20"><?php echo $item['item_name'] ?? "Unknown"; ?></h5>

                            <hr class="m-0">

                            <!-- product price -->

                            <table class="mr-2">

                                <tr class="font-rale">

                                    <td>R.R.P: <strike>£330.00</strike></td>

                                <tr class="font-size-16 text-danger">

                                    <td>Deal Price:</td>

                                    <td><span>£<?php echo $item['item_price'] ?? 0; ?></span></td>

                                <tr >

                                    <td class="font-rale text-danger">Save: £31.00</span></td>

                                </tr>

                            </table>

                            <!-- !product price -->

                            <hr class="m-0">

                            <!-- order-detail -->

                            <div id="order-details" class="font-rale d-flex flex-column text-dark">

                                <small>Delivered within 5-10 working days</small>

                                <small><i class="fas fa-map-marker-alt color-primar"></i>&nbsp;&nbsp; Built & dispatached from the UK</small>

                            </div>

                            <!-- !order-detail -->

                            <div class="row">

                                <div class="col-6">

                                    <!-- color -->

                                    <div class="color my-2">

                                        <div class="d-flex justify-content-between">

                                            <h6 class="font-baloo mr-1">Color:</h6>

                                            <div class="color1 p-2 rounded-circle mr-1"><button class="btn font-size-14"></button></div>

                                            <div class="color2 p-2 rounded-circle mr-1"><button class="btn font-size-14"></button></div>

                                            <div class="color3 p-2 rounded-circle mr-1"><button class="btn font-size-14"></button></div>

                                            <div class="color4 p-2 rounded-circle mr-1"><button class="btn font-size-14"></button></div>

                                            <div class="color5 p-2 rounded-circle mr-1"><button class="btn font-size-14"></button></div>

                                        </div>

                                    </div>

                                    <!-- !color -->

                                </div>

                                <div class="col-7">

                                    <!-- product qty section -->

                                    <div class="qty d-flex">

                                        <h6 class="font-baloo">Qty</h6>

                                        <div class="px-4 d-flex font-rale">

                                            <button class="qty-up border bg-light" data-id="pro1"><i class="fas fa-angle-up"></i></button>

                                            <input type="text" data-id="pro1" class="qty_input border px-2 w-50 bg-light" disabled value="1" placeholder="1">

                                            <button data-id="pro1" class="qty-down border bg-light"><i class="fas fa-angle-down"></i></button>

                                        </div>

                                    </div>

                                    <!-- !product qty section -->

                                    <!-- size -->

                                    <div class="size my-3">

                                        <h6 class="font-baloo">Size :</h6>

                                        <div class="d-flex justify-content-between w-75">

                                            <div class="font-rubik border p-2">

                                                <button class="btn p-0 font-size-12 mr-2">8GB RAM</button>

                                            </div>

                                            <div class="font-rubik border p-2 mr-2">

                                                <button class="btn p-0 font-size-12">16GB RAM</button>

                                            </div>

                                        </div>

                                    </div>

                                    <!-- !size -->

                                </div>

                            </div>

                        </div>

                    </div>

                    <div class="col-25">

                        <h6 class="font-rubik font-size-12">Product Description:</h6>

                        <hr>

                        <p class="font-rale font-size-14">

                            <?php echo $item['item_description'] ?? "To be confirmed"; ?>

                        </p>

                    </div>

                </div>

        </div>

        </section>

        <!-- !Animation Icons -->

        <!-- Title banner -->

        <div class="slider">

            <div class="slider-items">

                <div  class="item active">

                    <img src="./assets/products/Blue.png" />

                    <div class="caption">

                    </div>

                </div>

                <div  class="item">

                    <img src="./assets/products/Red.png" />

                    <div class="caption">

                    </div>

                </div>

                <div  class="item">

                    <img src="./assets/products/Red with MoBo.png" />

                    <div class="caption">

                    </div>

                </div>

                <div  class="item">

                    <img src="./assets/products/Purple.png" />

                    <div class="caption">

                    </div>

                </div>

                <div  class="item">

                    <img src="./assets/products/White.png" />

                    <div class="caption">

                    </div>

                </div>

            </div>

            <!-- slider controls -->

            <div class="left-slide"><</div>

            <div class="right-slide">></div>

            <!-- slider controls -->

        </div>

        </div>

        <!-- Title banner -->

        </div>

        <ul class="tabs">

            <li data-tab-target="#features" class="active tab">Features</li>

            <li data-tab-target="#specs" class="tab">Specifications</li>

            <li data-tab-target="#QandA" class="tab">Q&A</li>

        </ul>

        <div class="tab-content">

            <div id="features" data-tab-content class="active">

                <p></p>

                <div class="description"><img src="./assets/products/Gigabyte A320M-S2S.png" alt="">

                </div>

                <div class="content-header">

                    <b> A320M-S2H Motherboard</b>

                    <p></p>

                    <div class="content-text">

                        <?php echo $item['item_mobo_desc'] ?? "To be confirmed"; ?>

                    </div>

                    <div class="description2"><img src="./assets/products/tp-link.jpg" alt=""></div>

                    <div class="content-header">

                        <b> TP-Link WN881ND PCIe Network Interface Card</b>

                        <p></p>

                        <div class="content-text">

                            The TP Link TL-WN881ND Wireless N PCI Express 2.0 Adapter allows you to connect a desktop computer to a wireless network and access a high-speed Internet connection. Fully IEEE 802.11n complaint, the adapter provides wireless speeds of up to 300 Mbps, ideal for HD video streaming and online gaming.

                        </div>

                        <div class="description3"><img src="./assets/products/check.png" alt=""></div>

                        <div class="content-header"><b>Security is imperative and here at PC Kings we take that very seriously.</b></div>

                        <div class="content-text">

                            With that in mind we have included some additional applications to enhance your experience whilst keeping you secure. All our PCs include Avast anti Virus premium which keeps you safe online, Driver Booster to ensure your drivers and game components are up to date & Advanced SystemCare which will clear junk files and privacy traces.

                        </div>

                    </div>

                </div>

                <!-- Specs, Features & Q&A -->

                <!-- Specs, Features & Q&A -->

                <style>

                    img{

                        width: 100%;

                    }

                </style>

            </div>

            <div id="specs" data-tab-content>

                <p><table class="table1">

                    <tr>

                        <!-- CPU -->

                        <th scope="col" colspan="2">CPU</th>

                    </tr>

                    <td id="td" width="15%"><b>CPU brand</b></td>

                    <td><?php echo $item['item_brand'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <td id="td" width="15%"><b>Number of cores</b></td>

                    <td><?php echo $item['item_cores'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <td id="td"><b>Number of Threads</b></td>

                    <td><?php echo $item['item_threads'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <td id="td"><b>Frequency</b></td>

                    <td>3<?php echo $item['item_frequency'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <td id="td"><b>Turbo Core</b></td>

                    <td><?php echo $item['item_turbo_core'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <td id="td"><b>L3 Cache</b></td>

                    <td><?php echo $item['item_l3_cache'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !CPU -->

                    <!-- Memory -->

                    <th scope="col" colspan="2">Memory</th>

                    </tr>

                    <td id="td" width="15%"><b>Memory brand</b></td>

                    <td><?php echo $item['item_ram_brand'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <td id="td"><b>Description</b></td>

                    <td><?php echo $item['item_ram'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <td id="td"><b>Number of Modules</b></td>

                    <td><?php echo $item['item_ram_modules'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !Memory -->

                    <!-- Storage -->

                    <th scope="col" colspan="2">Storage</th>

                    </tr>

                    <td id="td" width="15%"><b>Brand</b></td>

                    <td>PC kings choice - industry recognised brand</td>

                    </tr>

                    <td id="td" width="15%"><b>Capacity</b></td>

                    <td><?php echo $item['item_storage'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !Storage -->

                    <!-- Motherboard -->

                    <th scope="col" colspan="2">Motherboard</th>

                    </tr>

                    <td id="td" width="15%"><b>Description</b></td>

                    <td><?php echo $item['item_mobo'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !Motherboard -->

                    <!-- Graphics -->

                    <th scope="col" colspan="2">Graphics</th>

                    </tr>

                    <td id="td" width="15%"><b>Description</b></td>

                    <td><?php echo $item['item_graphics'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !Graphics -->

                    <!-- PSU -->

                    <th scope="col" colspan="2">Power Supply</th>

                    </tr>

                    <td id="td" width="15%"><b>Description</b></td>

                    <td><?php echo $item['item_psu'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !PSU -->

                    <!-- Network -->

                    <th scope="col" colspan="2">Network (wireless)</th>

                    </tr>

                    <td id="td" width="15%"><b>Description</b></td>

                    <td><?php echo $item['item_wifi'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !Network -->

                    <!-- Case -->

                    <th scope="col" colspan="2">Case</th>

                    </tr>

                    <td id="td" width="15%"><b>Brand</b></td>

                    <td><?php echo $item['item_case_brand'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <td id="td" width="15%"><b>Color</b></td>

                    <td><?php echo $item['item_case_colour'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !Case -->

                    <!-- OS -->

                    <th scope="col" colspan="2">Operating System</th>

                    </tr>

                    <td id="td" width="15%"><b>Description</b></td>

                    <td><?php echo $item['item_os'] ?? "To be confirmed"; ?></td>

                    </tr>

                    <!-- !OS -->

                </table></p>

            </div>

            <div id="QandA" data-tab-content>

                <h1>Q&A</h1>

                <p>Let me tell you about me</p>

            </div>

        </div>

        <!-- !Specs, Features & Q&A -->

    <?php

    endif;

endforeach;

?>

<!-- !start #main -->

<script src="assets/Java/banner.js"></script>

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

<!-- Owl Carousel Js file -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js" integrity="sha256-pTxD+DSzIwmwhOqTFN+DB+nHjO4iAsbgfyFq5K5bcE0=" crossorigin="anonymous"></script>

<!--  isotope plugin cdn  -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/3.0.6/isotope.pkgd.min.js" integrity="sha256-CBrpuqrMhXwcLLUd5tvQ4euBHCdh7wGlDfNz8vbu/iI=" crossorigin="anonymous"></script>

<!-- !Custom Java -->

<script src="assets/Java/pd.js"></script>

<script src="HTML Template/assets/Java/index.js"></script>

I added bbcode [code][/code] tags around your code so that it will be displayed properly. Please do so yourself when posting code.

So, the posted code’s purpose is to display the requested item’s information, with an add to cart form. Previous comments still stand. If there is no item_id when this page gets requested, that’s an error. The code should also directly get just the data for the requested item.

The posted add to cart form should submit to the same page it is on, and ‘automatically’ propagate any existing get parameters. So, either the form processing code is what is doing the redirect and loosing the get parameter or all the posted code is inside of some other form. Nested forms are not valid, so the first opening <form ... tag encountered is what determines where the form will submit to and if any existing get parameters will be carried in the url.

Sponsor our Newsletter | Privacy Policy | Terms of Service