How to Define Variable PHP 7 for POST? [closed]

-1

I am trying to write information from a form on a base, however when I generate the following code below an error is presented in the file saying:

Notice: Undefined index: txtFirstName in C:\xampp\htdocs\begin\service.php on line 64
Notice: Undefined index: txtLastName in C:\xampp\htdocs\begin\service.php on line 65
Notice: Undefined index: ddlGender in C:\xampp\htdocs\begin\service.php on line 66
Notice: Undefined index: txtMinutes in C:\xampp\htdocs\begin\service.php on line 67
Notice: Undefined index: txtSeconds in C:\xampp\htdocs\begin\service.php on line 68
Notice: Undefined variable: mysqli_query in C:\xampp\htdocs\begin\service.php on line 74
Fatal error: Uncaught Error: Function name must be a string in C:\xampp\htdocs\begin\service.php:74 Stack trace: #0 {main} thrown in C:\xampp\htdocs\begin\service.php on line 74

The code I'm using to define the variables is this, which matches the above error lines.

I'm not a PHP Expert and found no response to my problem in the PHP doc. Could someone please help me. I'm using PHP7

$fname = htmlspecialchars($_POST['txtFirstName']);
$lname = htmlspecialchars($_POST['txtLastName']);
$gender = htmlspecialchars($_POST['ddlGender']);
$minutes = htmlspecialchars($_POST['txtMinutes']);
$seconds = htmlspecialchars($_POST['txtSeconds']);
$time = $minutes . ':' . $seconds;

My insert looks like this:

$mysqli_query($conect, 'INSERT INTO runners(first_name, last_name, gender, finish_time) values("$fname", "$lname", "$gender", "$time")');
    
asked by anonymous 25.09.2018 / 23:19

1 answer

1

First you need to send the data by the verb http POST I will leave an example using standard HTML form.

  

HTML form example

<form action="nome-do-seu-php.php" method="POST">
  <input type="text" placeholder="Primeiro nome" name="txtFirstName" />
  <input type="text" placeholder="Ultimo nome" name="txtLastName" />
  <select name="ddlGender">
     <option value="M">Masculino</option>
     <option value="F">Feminino</option>
  </select>
  <button type="submit"> Enviar dados</button>
</form>

Notice that in the form tag I put the method attribute with the POST value and that in input / select , I type the name attribute with the name that you will receive in $_POST / p>

Note: I have not put the last two fields of your script on the form, so you may be adding and testing them.

If you can not get the information and you want to treat it in two ways.

The first one is using isset and the second one is simpler and making use of coalesce ?? .

  

Example:

<?php
  // Exemplo do isset
  $fname = htmlspecialchars(isset($_POST['txtFirstName']) ? $_POST['txtFirstName'] : 'Valor Padrão');
  // Exemplo do coalesce
  $lname = htmlspecialchars($_POST['txtLastName'] ?? 'Valor Padrão');
    
25.09.2018 / 23:44