how to remove an elemetto from the array in php using a boto or equita to [duplicate]

-1

Good, I need to know how to remove an element from the array of a for loop in php using the unset () worse than this is executed when clicking on a button or a tag to > if someone gives me a solution

<?php
session_start();

if(isset($_GET['p'])){
$_SESSION['producto'][$_SESSION['contador']] = $_GET['p']; 
$_SESSION['contador']++;


}

if (isset($_GET['c'])){
    $_SESSION['cantidad'][$_SESSION['contador']] = $_GET['c'];
}

include '../conexion/server.php';

for ($i=0; $i < $_SESSION['contador']; $i++){
//echo "producto:".$_SESSION['producto'][$i]."-".$_GET['c'][$i]."<br>";

        $query = mysqli_query($conect,'SELECT * FROM Productos WHERE id='.$_SESSION['producto'][$i].'');

        while ($fila = mysqli_fetch_array($query)) {


            $cab = $_SESSION['cantidad'][$i+1];
            $total = $fila['Precio']*$cab;
                echo '<tr>';
                echo '<td>'.$fila['Nombre'].'</td>';
                echo '<center><td>';
                echo '<center><input type=number name=ja class="la ma" value='.$cab.'></center>';
                echo '</td></center>';
                echo '<td>'.$total.'</td>';
                echo '<td>';
                echo '<form method=get>';
                echo '<input type=hidden name=ya value='.$i.'>';
                echo '<button type=submit name="boton_borrar">borrar</button>';
                echo '</form>';
                echo '</td>';
                echo '</tr>';


                    if(isset($_GET['boton_borrar'])){

                                $v = $_GET['ya'];

                            unset($_SESSION['contador'][$v]);


                    }

        }


}

?>
<?php
session_start();

if(isset($_GET['p'])){
$_SESSION['producto'][$_SESSION['contador']] = $_GET['p']; 
$_SESSION['contador']++;


}

if (isset($_GET['c'])){
    $_SESSION['cantidad'][$_SESSION['contador']] = $_GET['c'];
}

include '../conexion/server.php';
    
asked by jose moya 15.10.2017 в 01:32
source

1 answer

0

a simple and simple solution for what you want to do, may be the following ...

In the HTML tag, you can add the onclick attribute with a JavaScript statement to execute an external PHP. If you want that PHP to run in the same window, you should use "location.href", whereas if you want it to run in a separate tab or window, you should use "window.open".

Example with "location.href":

<a onclick="location.href='borrar.php?borrar=valor'" href="#">Borrar</a>

Example with "window.open":

<a onclick="window.open('borrar.php?borrar=valor')" href="#">Borrar</a>

You can also use JQuery Ajax to set an "onclick" event in a " <script> " element so that it executes a request asynchronously so that you do not leave the tab, and you can even make FrontEnd modifications inside of that same event.

Example:

<script>
    $("a.borrar").on("click", function(){
        url = "borrar.php";
        data = { borrar : "valor" };
        $.ajax({
            url: url,
            data: data,
            type: "GET",
            success: function (e) {
                alert("valor eliminado!");
            }
        });
    });
</script>

<a class="borrar" href="#">Borrar</a>

I clarify that this is to execute the PHP file by clicking on a label " <a> " as you said in the question. What you want to do in PHP already happens to be your idea how you want to eliminate it.

I hope this is useful, greetings!

    
answered by 15.10.2017 в 17:22