Notice: Undefined index using POST in php

0

<!DOCTYPE html>
<html lang="es">
<head>
	<meta charset="UTF-8">
	<title>Enviar Datos</title>
</head>
<body>
	<form action="procesaDatos.php" method="post">
  Nombre:
			<br>
			<input type="text" name="nombre">
			<br>
			<input type="submit" value="buscar">

		</form>

<?php


$nombre=$_POST['nombre'];

echo "tu nombre es:", $nombre;





?>

Having this code, the html when testing it works correctly but the php file marks me:

  

Notice: Undefined index: name in   C: \ xampp \ htdocs \ tests \ procesaDatos.php on line 3

your name is:

    
asked by nancy windsor 08.10.2017 в 17:50
source

3 answers

1

You have to give the id="name" in your html code

<input type="submit" value="buscar" id="nombre">

And the PHP code is NOT ASI echo "tu nombre es:", $nombre; It's not a comma, it's like this:

echo "tu nombre es:" . $nombre;
    
answered by 09.10.2017 в 02:27
0

You must place the attribute name in the input of type submit, and the method isset() to validate what you sent by $_POST[]

Ex:

<input name="btnEnviar" type="submit" value="Enviar"/>

if(isset($_POST['btnEnviar´])) {
   $nombre=$_POST['nombre'];
}

That should solve the problem

    
answered by 23.07.2018 в 00:05
0

There are several problems in your code, first of all you are assigning a value of an undefined variable to $ name in

$nombre=$_POST['nombre'] // <- Incorrecto

Try to validate if the POST variable has content with an if and  the isset function that will return true if the POST variable is already initialized, also, to concatenate in PHP the comma is not used, for that you should put a point, I'll give you an example:

<?php
     if( isset($_POST['nombre']) ) {
        $nombre = $_POST['nombre'];
        echo "Tu nombre es: ".$nombre;
     }
?>
    
answered by 09.09.2018 в 03:55