Skip to content
Advertisement

How to refresh page and send variable

i have a php page where i press a button to edit on another page the mysql db records: page name ‘distintaBase.php’ here there is a list of names that are products name. Each product cointains a list of components. When i press the edit button for one product i’m redirect to the new page ‘modifDistBase.php’.

<a href="modifDistBase.php?dist_base=<?php echo $dist_base; ?>"></a>

the $dist_base variable is passed to the new page. I use it on the page title

<h2>Modifica distinta base <?php echo $_GET['dist_base']; ?></h2>

and then to load the mysql db

<?php 
                $dist_base = $_GET['dist_base'];
                $sql = "SELECT $dist_base.Id, $dist_base.Designator, $dist_base.Quantity, $dist_base.Description, $dist_base.Package, 
                                $dist_base.Manufacturer, $dist_base.Pn_Manufacturer, $dist_base.Pn_Utterson, $dist_base.Mounted
                                FROM $dist_base";                                                                                                     
?>

pressing ADD button i add a new component

if(isset($_POST['add_item'])){
                    $designator = $_POST['designator'];
                    $quantity = $_POST['quantity'];
                    $description = $_POST['description'];
                    $package = $_POST['package'];
                    $manufacturer = $_POST['manufacturer'];
                    $pn_manufacturer = $_POST['pn_manufacturer'];
                    $pn_utterson = $_POST['pn_utterson'];
                    $mounted = $_POST['mounted'];
                    $sql = "INSERT INTO $dist_base (designator,quantity,description,package,manufacturer,pn_manufacturer,pn_utterson,mounted)
                                            VALUES ('$designator','$quantity','$description','$package','$manufacturer','$pn_manufacturer','$pn_utterson','$mounted')";

                 if ($conn->query($sql) === TRUE) {                         
                        echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';
                    } else {
                        echo "Errore: " . $sql . "<br>" . $conn->error;
                        }
                }           

there is something wrong probably on the javascript.

echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';

the new component is added to the database (for example with #5 id) but do not appear on the page modifDistBase.php

if i press refresh on the browser or f5 now i can see the new component (#5 id) but on the database a new one is added #6 with the same items as #5

PS – header is already sent by menĂ¹ page – have tried window.location.href with same result

Advertisement

Answer

there are some thing to fix on your code

Note the javascript you output with

echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';

will be precisely

<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>

Without checking the correctness of the js related to your goal, it’s immediate to see you should write instead

echo '<script>window.location.replace("modifDistBase.php?dist_base="' . $dist_base . '")</script>';

to inject the php value into the javascript string. (you’ve also forgot a double quote b.t.w)

If you have doubts regarding your javascript, you can inspect the page on the browser and look at it (right click and “inspect element” or “analyse element”).

Mind it is not recommended that you take the user input and use it as it is in an SQL instruction. You should do some sanification and use a prepared statement or escape the values before putting them in the sql.

That said, the javascript redirection you do looks a bit odd, maybe you can redirect from the php script without asking the client to do so. Look at the header php function for instruction about redirection done server side.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement