How to receive data from an html form with PHP

Asked

Viewed 1,287 times

-1

Good afternoon, I am new in this area and I am having problems to receive data from my html form with my PHP code. My problem is I’m using for the name of Inputs the following format name="date[Resume][name]", but when I enter the input name in my Php code it does not receive the data. Follow the code I’m using is others I’ve tried as possible solutions:

<html>
<head>
<title></title>
</head>
<body>

    <form id="formAddCurriculo" class="form-default" enctype="multipart/form-data" method="post" action="conexão.php" accept-charset="utf-8">

    <div style="display:none;"><input type="hidden" name="_method" value="POST" /></div>

<h5 class="title-field whit-padding">Dados Pessoais</h5>

<fieldset class="clearfix mbottom sides-margin">
    <div class="input text">
        <label for="CurriculoName" class="required">Nome Completo</label>
          <input name="data[Curriculo][name]" type="text" class="field field-big" maxlength="255" id="CurriculoName" /></div>

    <div class="input text">
        <label for="CurriculoRg">RG</label>
            <input name="data[Curriculo][rg]" type="text" class="field field-medium" maxlength="14" id="CurriculoRg" /></div>

    <div class="input text required">
        <label for="CurriculoCpf" class="required">CPF</label>
        <input name="data[Curriculo][cpf]" type="text" class="field field-medium cpf" maxlength="50" id="CurriculoCpf" /></div>

    <fieldset class="clearfix sides-margin">
    <div class="submit"><input class="to-upper fright btn-green ts" type="submit" value="Cadastrar" /></div> 

        </fieldset>

                            </form>

</body>
</html>

PHP

<?php
$servername = "localhost";
$database = "**********";
$username = "***********";
$password = "************";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";


// RECEBENDO OS DADOS PREENCHIDOS DO FORMULÁRIO !


$data[Curriculo][name] = $_POST ['$data[Curriculo][name]']; 
$data[Curriculo][rg] = $_POST ['data[Curriculo][rg]']; 
$data[Curriculo][cpf]f = $_POST ['data[Curriculo][cpf]']; 


$Usuarios = "INSERT INTO Usuarios (ID,NomeCompleto, RG, CPF)VALUES((Null,'$data[Curriculo][name]','data[Curriculo][rg]','data[Curriculo][cpf]')";

if (mysqli_query($conn, $Usuarios)) {
      echo "New record created successfully";
} else {
      echo "Error: " . $Usuarios . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

However it does not receive the data from Inputs, I know it may seem obvious but I could not find the solution in the PHP documentation, and I would not like to change the name of the Inputs because I have the javascript validation codes and the stylesheet written in this format

  • php shows some error or just the data in the database is blank?? I noticed that the quotes and the dot and comma are missing at the end of your sql code.

  • After receiving the data in php try a var_dump($_POST); and post here so we can see if you’re receiving the data or not.

  • Simple debug. At the beginning of the php file, right after the opening tag put the var_dump quoted by @Joãovictorsouza

  • Juliano, if you put it like this in HTML: <input name="data['Curriculo']['rg']" type="text", with simple quotation marks? In PHP, this is how: $data["Curriculo"]["name"] = $_POST['$data[Curriculo][name]'];

  • Why not simplify and <input name="curriculo_rg" type="text" ? In PHP $_POST['curriculo_rg'];

  • After I add the var_dump($_POST);, it displays the message that is receiving the data :Connected successfullyarray(2) { ["_method"]=> string(4) "POST" ["data"]=> array(2) { ["Curriculum"]=> array(62) { ["name"]=> string(18) "Juliano corolesqui" ["rg"]=> string(8) "58558507" ["Cpf"]=> string(14) "082.384.288-81"} } New record successfully created Why in the database save the following data in the fields Array[name] Array[rg] Array[Cpf] 0000-00-00

  • try $_POST['data']['Resume']['name']

  • your code is with several syntax errors.

Show 3 more comments

1 answer

0

You are using the name of the inputs as if they were matrices. Do you really need this? Will you change the data of more than one user? If you don’t need to, try to simplify the name of the inputs. You could leave their name as follows:

<input name="name" type="text" class="field field-big" maxlength="255" id="CurriculoName" />

<input name="rg" type="text" class="field field-medium" maxlength="14" id="CurriculoRg" />

<input name="cpf" type="text" class="field field-medium cpf" maxlength="50" id="CurriculoCpf" />

Whereas you simplified the name in the inputs, you could take the value of the input in PHP this way:

$data["Curriculo"]["name"] = $_POST["name"]; 
$data["Curriculo"]["rg"] = $_POST["rg"]; 
$data["Curriculo"]["cpf"] = $_POST["cpf"];

But again you will be using matrix. If not necessary. You can also simplicate the variables.

$name = $_POST["name"]; 
$rg = $_POST["rg"]; 
$cpf = $_POST["cpf"]; 
  • Less is more, simplify your code

Browser other questions tagged

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