How to Set PHP 7 Variable for POST?

Asked

Viewed 298 times

-1

I am trying to record information from a form on a basis, but when I Gero the following code below is presented an error in the file saying that:

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 am using to define the variables is this, which is corresponding to the above error lines.

I am not a PHP Expert and I didn’t find any answers to my respective problem in the PHP doc. Could someone please help me. I’m using the 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 Internet is like this:

$mysqli_query($conect, 'INSERT INTO runners(first_name, last_name, gender, finish_time) values("$fname", "$lname", "$gender", "$time")');
  • 1

    undefined index means that the array $_POST does not contain the indexes you are looking for. You need to check if there is anything in this array. You can use isset or the operator null coalesces (??)

1 answer

1


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

Example HTML form

<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>

Note that on the tag form I put the attribute method with the value POST and that we input/select report the attribute name with the name you will receive on $_POST

Note: I didn’t put the last two fields of your script in the form, so you might be adding and testing.

Should you happen not to receive the information and you want to treat there are two ways.

The first is by making use of the isset and the second that is simpler and making use of the coalition ??.

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');
  • Hiago, thanks bro, God bless you there for help. .

Browser other questions tagged

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