I have 3 php files: the first with a form and its validation, the second that stores those values in the db and the third contains the domain, etc. I have tried everything to pass those variables values from the first to second php ex: $_POST
, $_Get
and isset()
as display in the Save.php
.
But the values are " " or null
(empty) because I echo them in the Save.php
and nothing appears. Just gives me messages for each variable as follows - Notice: Undefined index: lname in C:\xampp\blablabla\Save.php on line 5
.
The connection is working, the query is working, the validation is working, the form is working. The only problem is when I call
if($valid)
{
header('Location: Save.php');
exit();
}
main.php
<?php
$nombreErr = "";
$nombre = "";
$apellidoErr = "";
$apellido = "";
$motivosErr = "";
$motivos = "";
$idEstudianteErr = "";
$idEstudiante = "";
$segundoAppellidoErr = "";
$segundoAppellido = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$valid = true;
if (empty($_POST["fname"])) //Nombre Requerido y Solo letras y espacios en blanco
{
$nombreErr = "Nombre requerido";
$valid = false;
}
else
{
$nombre = test_input($_POST["fname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$nombre))
{
$nombreErr = "Solo letras y espacios en blanco";
$valid = false;
}
}
if (empty($_POST["lname"])) //Apellido Requerido y Solo letras y espacios en blanco
{
$apellidoErr = "Apellido Requerido";
$valid = false;
}
else
{
$apellido = test_input($_POST["lname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$apellido))
{
$nombreErr = "Solo letras y espacios en blanco";
$valid = false;
}
}
if (empty($_POST["Slname"])) //Segundo apellido solo letras y espacios en blanco
{
$segundoAppellido = "";
$valid = false;
}
else
{
$segundoAppellido = test_input($_POST["Slname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$segundoAppellido))
{
$segundoAppellidoErr = "Solo letras y espacios en blanco";
$valid = false;
}
}
if (empty($_POST["studentId"])) //Numero estudiante solo numeros y rayas
{
$idEstudianteErr= "";
$valid = false;
}
else
{
$idEstudiante = test_input($_POST["studentId"]);
if (!preg_match("/^[0-9-]*$/",$idEstudiante))
{
$idEstudianteErr = "Solo numeros y \"-\"";
$valid = false;
}
}
if (empty($_POST["Motive"])) //Motivos Requeridos
{
$motivosErr = "Motivo(s) Requerido(s)";
$valid = false;
}
if($valid)
{
header('Location: Save.php');
exit();
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<input type="hidden" name="depName" value="Admisiones">
<input type="hidden" name="personStat" value="Espera">
<class="input1">FirstName :<input type="text" name="fname" value="<?php echo $nombre;?>">
<span class="error">* <?php echo $nombreErr;?></span> <br>
<class="input2">LastName :<input type="text" name="lname" value="<?php echo $apellido;?>">
<span class="error">* <?php echo $apellidoErr;?></span> <br>
<class="input3">Second LastName :<input type="text" name="Slname" value="<?php echo $segundoAppellido; ?>">
<span class="error"><?php echo $segundoAppellidoErr; ?></span> <br>
<class="input4">Student Id :<input type="text" name="studentId" value="<?php echo $idEstudiante;?>">
<span class="error"><?php echo $idEstudianteErr; ?></span> <br>
<select name="Motive[]" multiple="MULTIPLE">
<?php
$sql1="SELECT categoryName, estimationTime FROM Category where depName='Asistencia Economica'";
$result1=mysqli_query($con, $sql1);
while($row1 = mysqli_fetch_array($result1))
{
echo"<option value=".str_replace(" ", "_", $row1['categoryName']) .":". $row1['estimationTime'].">".$row1['categoryName']."</option>";
}
?>
</select>
<span class="error">* <?php echo $motivosErr;?></span> <br>
<input class="button" type="submit" value="Registrate">
</form>
Save.php
<?php
include('Connection.php');
$firstname = isset($_POST['fname']);
$lastname = mysqli_real_escape_string($con, $_POST['lname']);
$secondLastName = mysqli_real_escape_string($con, $_POST['Slname']);
$studentId =mysqli_real_escape_string($con, $_POST['studentId']);
$depNum= mysqli_real_escape_string($con, $_POST['depName']);
$status= $_POST['personStat'];
$mymotives= $_GET['Motive'];
$motivestring= "";
$motiveTime= 0;
echo $firstname . "where are you";
echo $lastname;
echo $secondLastName;
echo $studentId;
echo $depNum;
echo $status;
echo $mymotives;
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql ="INSERT INTO List (depName, personName, personLast, secondLast, stuId, personStatus, manyMotive, categoryTime)
VALUES ('$depNum','$firstname','$lastname', '$secondLastName','$studentId', '$status', '$motivestring', '$motiveTime')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
?>