HTML5 and PHP form all on the same page

Asked

Viewed 223 times

-1

How to process these tasks before the form is saved to the database?

CASE 01: How to prevent the boss not be repeated in the visitor?

CASE 02: How to determine winner or draw?

CASE 03: Determined the CASE 02, how to record it in the hidden field guy?

CASE 04: Having winner, how to record it in the hidden field outworking?

CASE 05: How to display message from SUCCESSFULLY RECORDED FORM, only if all requests are met?

<!DOCTYPE html>
<html>
<?php
include("bd.php"); //Conectando com o MySQL
?>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Registro de resultado dos jogos</title>

    <link type="text/css" rel="stylesheet" media="screen" href="estilos.css" />
</head>
<body>
    <div id="site"> 
        <form method="post" id="insere_jgfeito" align="center" enctype="multipart/form-data"> <!-- ÍNICIO DO USUÁRIO PREENCHENDO FORMULÁRIO -->

            <td>
            <tr>
                <span><strong>Inserir Resultados<strong></span>
            </br>
            </tr>

            <tr>
            <td>
<select required name="campeonato"/>
    <option value="" disabled="disabled" selected="selected">Campeonato</option>
    <?php
        $cst_campeonato = mysql_query("SELECT `id`, `campeonato` FROM `tbl_campeonatos` WHERE `status` = 1 ORDER BY `id` ASC");
            while($campeonatos = mysql_fetch_array($cst_campeonato)){ ?>
    <option value="<?php echo $campeonatos["id"];?>"><tr><?php echo $campeonatos["campeonato"];?></tr></option>
    <?php } ?>
<select>            </td></br>
            <td>
            <label for="data-atual">Realizado em...</label>
            <input type="date" name="data-atual" id="data-atual" value="<?=date('d/m/Y')?>" required>
            </td>
            </tr>
            </td></br>
            <tr>
            <td>
            <select required name="mandante"/>
                <option value="" disabled="disabled" selected="selected">Mandante</option>
                <?php
                    $cst_competidor = mysql_query("SELECT `tbl_jogadores`.`tecnico` AS tecnico, `tbl_competidor`.`nome` AS nome FROM `tbl_competidor` INNER JOIN `tbl_jogadores` ON `tbl_competidor`.`id_competidor` = `tbl_jogadores`.`tecnico` WHERE `tbl_competidor`.`status` = 1 GROUP BY `tbl_jogadores`.`tecnico` ORDER BY `tbl_competidor`.`nome` ASC");
                    while($tecnicos = mysql_fetch_array($cst_competidor)){ ?>
                <option value="<?php echo $tecnicos["tecnico"];?>"><tr><?php echo $tecnicos["nome"];?></tr></option>
                <?php } ?>
            <select>
            <input type="number" min="0" max="9" name="mscore" required="required">
            </td>
            <td>
            <input type="number" min="0" max="9" name="vscore" required="required">
            <select required name="visitante"/>
                <option value="" disabled="disabled" selected="selected">Visitante</option>
                <?php
                    $cst_competidor = mysql_query("SELECT `tbl_jogadores`.`tecnico` AS tecnico, `tbl_competidor`.`nome` AS nome FROM `tbl_competidor` INNER JOIN `tbl_jogadores` ON `tbl_competidor`.`id_competidor` = `tbl_jogadores`.`tecnico` WHERE `tbl_competidor`.`status` = 1 GROUP BY `tbl_jogadores`.`tecnico` ORDER BY `tbl_competidor`.`nome` ASC");
                    while($tecnicos = mysql_fetch_array($cst_competidor)){ ?>
                <option value="<?php echo $tecnicos["tecnico"];?>"><tr><?php echo $tecnicos["nome"];?></tr></option>
                <?php } ?>
            <select>
            </td>
            </tr>   
            <input type="hidden" value="?" name="tipo"/>
            <input type="hidden" value="?" name="resultado"/>
            <input type="hidden" value="1" name="status"/>
            <input type="hidden" name="acao" value="enviado" />
            <button type="submit">Gravar</button>
        </form> <!-- FIM DO FORMULÁRIO -->
    </div>
<?php 
/* RECEBENDO OS DADOS PREENCHIDOS DO FORMULÁRIO!
OBS: Para fins didáticos, todas as variavés utilizarão o prefixo "rcb_" referente a "recebe valor de" */

$rcb_campeonato = $_POST ["campeonato"];    // Captura o (select) com nome: "campeonato" referente ao "id_campeonato"
$rcb_data_atual = $_POST ["data-atual"];    // Traz (input date) com nome: "data-atual" 
$rcb_mandante   = $_POST ["mandante"];      // Captura o participante de nome: "mandante"
$rcb_mscore = $_POST ["mscore"];            // Recebe número entre 0 e 9 de: "mscore"
$rcb_vscore = $_POST ["vscore"];            // Recebe número entre 0 e 9 de: "vscore"
$rcb_visitante  = $_POST ["visitante"];     // Captura o participante de nome: "mandante"
$rcb_tipo   = $_POST ["tipo"];              // Campo oculto de valores 0=Empate, 1=Vitória do mandante ou 2=Visitante
$rcb_resultado = $_POST ["resultado"];      // Deve gravar o ID vencedor (mandante ou visitante) 
$rcb_status = $_POST ["status"];            // Se tudo estiver OK, grava sempre o campo com valor 1.


//Como gravar esses dados no banco de dados?


//Como referenciar a tabela do banco de dados?


$query = "INSERT INTO `tbl_jogos` ( `Null` , `id_campeonato`, `dt_partida` , `mandante` , `visitante` , `mscore` , `vscore` , `tipo` , `resultado`, `status`) 
VALUES ('$rcb_campeonato', '$rcb_data_atual', '$rcb_mandante', '$rcb_visitante', '$rcb_mscore', '$rcb_vscore', '$rcb_tipo', '$rcb_resultado', '$rcb_status')";

echo "Seu resultado foi gravado com sucesso!";
?>
</body>
</html>
  • Use conditional structures..

  • Try separating html from php, and in php create the persistence classes

1 answer

0


CASE 01: just create a parole and fire some error if you pass...

if($rcb_visitante == $rcb_mandante) {
    echo "O mandante não pode ser igual ao visitante.";
}

CASE 02: just add two variables: $vencedor (string) and $empate (Boolean) and populate them from one more conditional. Ps.: for the following cases, I have created more variables to assist in printing: $tipo and $resultado.

$vencedor = $empate = false;
$tipo = $resultado = "?";
if($rcb_mscore == $rcb_vscore) {
   $empate = true;
   $tipo = "0";
} elseif($rcb_mscore > $rcb_vscore) {
   $vencedor = $resultado = $rcb_mandante;
   $tipo = "1";
} else {
   $vencedor = $resultado = $rcb_visitante;
   $tipo = "2";
}

CASE 03: in order to carry out this operation, this validation process must come BEFORE of the form display, so that in the section where we write the field tipo we can dynamically populate it, for example:

<input type="hidden" value="<?=$tipo?>" name="tipo"/>

CASE 04: in the same way as the previous stage...

<input type="hidden" value="<?=$resultado?>" name="resultado"/>

CASE 05: at the end of all processes, it will be necessary to have a variable indicating that the form has passed the validation process. In this case, I will take this opportunity to put the whole code of how this process would look. Note: I based myself solely on the structure you were following in this exercise, but this structure and way of working is not a good practice. The ideal is to separate the server-side from the client-side, that is, your form response could be an AJAX request. However, at the level of the thing I believe it is not yet time to talk about it, hehe. I recommend learning: Javascript > AJAX > jQuery > jQuery.AJAX + PHP. It will change your view on the subject a bit. Anyway, let’s go code:

https://pastebin.com/hXr0nGi

Anyway, however, the entire structure of your HTML is full of errors... Have a little more patience in your studies, I think you’re going too fast... Just a word of advice. As I said, I followed what you were trying to do (with server-side validations), but these validations will only occur when the user submits the form (the page will be reloaded). If your intention is to prevent this, your validation will need to be client-side (Javascript). I can elaborate an answer by doing the same things with jQuery (to explain faster, because there is time... haha).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.