php - Delete words from a String using buttons

0

Good afternoon I tell you I would like to delete words from a text string using buttons, I have the following variable $frase = "Juan tiene un Auto, Maria una Moto y Carlos una Bicicleta"; I want to modify it using the following form.

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3- 
 typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>  
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"> </script>

 <form method="post" action="borrar.php">
 <input type="hidden" name="borra_auto" id="borra_auto" value="Auto">
 <button id="boton1" name="boton1" class="btn btn-success">Auto</button>        
 </form>
 <br>
 <form method="post" action="">
 <input type="hidden" name="borra_moto" id="borra_moto" value="Moto">
 <button id="boton2" name="boton2" class="btn btn-warning">Moto</button>        
 </form>
 <br>
 <form method="post" action="">
 <input type="hidden" name="borra_bici" id="borra_bici" value="Bicicleta">
 <button id="boton3" name="boton3" class="btn btn-info">Bicicleta</button>  
 </form>

the code of the file delete.php is as follows:

<?php
session_start();
$frase = "Juan tiene un Auto, Maria una Moto y Carlos una Bicicleta"; 

if (isset($_POST["boton1"])) {
$_SESSION["frase"] = str_replace($_POST["borra_auto"], "", $frase);
}

if (isset($_POST["boton2"])) {
$_SESSION["frase"] = str_replace($_POST["borra_moto"], "", $frase);
}

if (isset($_POST["boton3"])) {
$_SESSION["frase"] = str_replace($_POST["borra_bici"], "", $frase);
}

echo $_SESSION["frase"];
?>

I tried to press the buttons successively but one value replaces the other, my intention is to see a result like this:

  

$ frase="Juan has one, Maria one and Carlos one";

For your help, thank you.

    
asked by Julian Martinez 05.07.2018 в 23:19
source

1 answer

0

The issue is that $ phrase="Juan has a car, Maria has a motorcycle and Carlos has a bicycle"; every time you send the form it's still the same you could use: $ _SESSION ['string_phrase']="Juan has an Auto, Maria a Moto and Carlos a Bicycle"; thing that will remain the last modified

    
answered by 05.07.2018 в 23:29