Help with using $_POST in PHP

Asked

Viewed 61 times

0

Hello, I’m having the following mistake:

Notice: Undefined index: dataInicio in C: xampp htdocs Dashboard Dashboard.php on line 124.

Apparently I’m not making the right use of the $_POST, but I don’t know where I’m going wrong, can anyone help me? I’ll put the code to clarify how everything is being defined:

Here in PHP I get the $_POST index :

// Filtro data

$dataHumanaFim = "";
$dataHumanaInicio = "";

$dataHumanaInicio = $_POST['dataInicio'];
$dataHumanaFim = $_POST['dataFim'];

$sqlFiltroData = 'SELECT * from devices where first_time between "'.mktime($dataHumanaInicio).'" and "'.mktime($dataHumanaInicio).'"';
$filtroData = $pdo->query($sqlFiltroData);
//var_dump($filtroData);

This is where I require the values that should go for $_POST:

<form method="POST" action="dashboard.php">
  <div class="col-4">

  <label for="dataInicio">De:</label>
  <input type="text" id="dataInicio" name="dataInicio">
  <label for="dataFim">Até:</label>
  <input type="text" id="dataFim" name="dataFim">

</div>

  <button type="submit" class="btn btn-primary btn-alert" onclick="filtroData()" id="pesquisaFiltro" value="Pesquisa" name="submit">Pesquisar</button>
</form>

And here is the function I do in javascript/jquery for the button... :

$(function filtroData(){
//Função para filtro por data
$('#pesquisaFiltro').click(function(){ 
document.getElementById("filtroData").removeAttribute("hidden");
});
});

Can someone tell me where I’m going wrong or where I can verify a better way to do what I’m trying to do?

  • $dataHumanaInicio = $_POST['dataInicio']; the error actually happens on this line and on the 125 line: $dataHumanaFim = $_POST['dataFim'];

  • I tested here and did not error: https://repl.it/@Ronaldovasques/Insistentunequaledbytes . Error must be elsewhere.

  • Something else you don’t use onclick with submit.

  • I think I know where the error is, I’m doing everything on the same page, not separate as you show there in the test example, maybe if I play PHP in another file and change the action to that file the error sum... I’m gonna test

  • Well, I changed the action to "send.php" and put the variables there as well as in your example, and the error follows the same, only changed the location... : Notice: Undefined index: dataInicio in C: xampp htdocs Dashboard envia.php on line 7 Notice: Undefined index: dataFim in C: xampp htdocs Dashboard envia.php on line 8

  • Is the code too big? If not, send a repl.it with your code to be analyzed.

  • It’s kind of big yes, I think we can not reproduce everything in repl.it ... There is some other way ?

  • Github, but I work. I spend coffee breaks here at Stackoverflow. If it’s too big I won’t have to reconcile things.

Show 3 more comments

1 answer

1


I think I understand, you put in the same file, so if you do not send the POST it searches the index and does not find.

And since you’re opening the page to display the form, php tries to fetch the index in the post array and cannot find it.

Smpre use isset() to check if and var is set:

if(isset($_POST['dataInicio']) && isset($_POST['dataFim'])) {
    $dataHumanaInicio = $_POST['dataInicio'];
    $dataHumanaFim = $_POST['dataFim'];
}

This way php will see that the variables do not exist yet and follows the game.

In short, if you submit the form it will work the way it is, the problem is that you cannot open the page to submit the form.

Putting the Cód up will work.

  • 1

    Perfect friend, this is exactly what was missing. Thank you very much for the explanation, it was very clear, I did not understand right the functioning of the isset and you clarified. Very good!!!

Browser other questions tagged

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