Send input content to database

Asked

Viewed 57 times

0

I’m having a problem trying to get the values of these inputs to be sent to a database, my knowledge of php is very basic, I tried searching in various forums to see if I found something but I was not successful. I would like help to do this.

<div id="data">
    <section>

        <label for="">Nome:</label>
        <br>
        <input type="text" id="nome" required>
        <br>
        <label for="">Sobrenome:</label>
        <br>
        <input type="text" id="sobrenome" required>
        <br>
        <label for="">Cargo:</label>
        <br>
        <select id="cargo">
            <option value="desenvolvedorFront">Desenvolvedor - Front-End</option>
            <option value="desenvolvedorBack">Desenvolvedor - Back-End</option>
            <option value="designerGrafico">Designer Gráfico</option>
            <option value="auditor">Auditor</option>
            <option value="analistaFinanceiro">Analísta Financeiro</option>
        </select>
        <br>
        <button id="confirmar" onclick="confirmaInputData()">Confirmar</button>
        <div id="outputData"></div>
    </section>
</div>

1 answer

4


Your code is not very organized in the HTML section. You can’t get an exact idea of your problem. From what you have reported, this Javascript function is not required. Only if you might want to use AJAX, but I still don’t see much basis in this situation. I’ll try to help you. See if this code fits you. Try to build on it to elaborate your project and solve your problem.

<div id="data">
  <section>
    <form action="enviar-dados.php" method="post">

      <label for="nome">Nome:</label>
      <br>
      <input type="text" id="nome" name="nome" required>

      <br>
      <label for="sobrenome">Sobrenome:</label>
      <br>
      <input type="text" id="sobrenome" name="sobrenome" required>
      <br>

      <label for="cargo">Cargo:</label>
      <br>
      <select id="cargo" name="cargo">
        <option value="desenvolvedorFront">Desenvolvedor - Front-End</option>
        <option value="desenvolvedorBack">Desenvolvedor - Back-End</option>
        <option value="designerGrafico">Designer Gráfico</option>
        <option value="auditor">Auditor</option>
        <option value="analistaFinanceiro">Analísta Financeiro</option>
      </select>

      <br>
      <button id="confirmar" type="submit">Confirmar</button>

    </form>

  </section>
</div>


<!-- ARQUIVO ENVIAR-DADOS.PHP -->

<?php
  
  $nome = $_POST["nome"];
  $sobrenome = $_POST["sobrenome"];
  $cargo = $_POST["cargo"];

  $insert = "INSERT INTO tbl_cadastro(nome, sobrenome, cargo) 
	VALUES('$nome','$sobrenome','$cargo')";
	
	$insert_execucao = mysqli_query($conexao, $insert);

?>

  • In the code we create a form (which will send the data you want to register in the database to the file send-data.php). It is this PHP file that you perform the query of entering the data in the database. We specify this php file in the "action" of the form.

  • We added the "name" in the inputs, pq is by name that the PHP file will recognize the fields coming from the form.

  • In the PHP file (send-data.php), we receive the data from the form and pass them to variables. (Remembering that I am basing that we have created a PHP code on the same page as your HTML code file)

  • Then (assuming that the name of your table in the database was "tbl_registration" and had the fields name, surname and position) we performed an Index of the data coming from the form in this table).

  • Finally we executed the query and completed the insertion with mysqli_query.

  • Clarifying that the variable "$connected" must store your connection data with the database.

  • Maybe (maybe not, you really need to) have a require_once, at the beginning of the php file, calling that your file that makes the connection.

  • Your code posted doesn’t have much information. Maybe by that my answer you can have a north (not the north of Game of Thrones, rs) to resolve your situation.

  • Thanks.

  • 1

    Okay, thank you so much I managed to solve my problem using your explanation.

  • Ball show, @Joãopedroantoniazi , Continue on this path. May the force be with you!

  • And with you too!

Browser other questions tagged

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