Skip to content
Advertisement

how can i use only one file instead of 4 php files that are basically the same?

i basically have a simple porgram to count how many times a click a specific button and then send it to mysql, but for each button i have diferent tables and separated files. I would like to know if there is any way to join the 4 files into only one, since it is repeating the same thing 4 times in diferent files. Here is my code:

index.php

<!DOCTYPE html>
<html>
<head>
    <title>Fct</title>
</head>
<body>
<form  action="inserir.php" method="post">
   <button name="A" type="submit" value="A">Senha A</button>
</form>

<form  action="inserir2.php" method="post">
   <button name="B" type="submit" value="B">Senha B</button>
</form>

<form  action="inserir3.php" method="post">
   <button name="C" type="submit" value="C">Senha C</button>
</form>

<form  action="inserir4.php" method="post">
   <button name="D" type="submit" value="D">Senha D</button>
</form>

</body>
</html> 

and then the file to insert into mysql witch is inserir.php;

<?php
include 'bdados.php';

$A = $_POST['A'];

$query = "INSERT into `tipo1`(`senhaa`) VALUES (  '$A' )";


mysqli_query($dbconn, $query);

header("location: index.php");



 ?>

basically i have 4 “inserir.php” and i think i could shrink those 4 files in only one, i just dont know how. All help is much apreciated 🙂

Advertisement

Answer

Add submit buttons with same name but different values.Retrieve the value by $_POST['name']

HTML

<form  action="inserir.php" method="post">
   <button name="val" type="submit" value="A">Senha A</button>
<button name="val" type="submit" value="B">Senha B</button>
<button name="val" type="submit" value="C">Senha C</button>
<button name="val" type="submit" value="D">Senha D</button>
</form>

PHP

<?php
include 'bdados.php';

$val = $_POST['val'];

$query = "INSERT into `tipo1`(`senhaa`) VALUES (  '$val' )";


mysqli_query($dbconn, $query);

header("location: index.php");



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